Tips For a Better Redux Architecture: Lessons for Enterprise Scale

Hashnode Original


Write your comment…

This comment has received 2 appreciations.

If you were using redux-define you could reduce the boilerplate of defining action types.

Especially when using status suffixes like ERROR or SUCCESS, it can make a big difference.

Turning your example

export const ADD_TODO = 'todos/ADD_TODO'
export const ADD_TODO_SUCCESS = 'todos/ADD_TODO_SUCCESS'
export const ADD_TODO_FAILURE = 'todos/ADD_TODO_FAILURE'


export const ADD_TODO = defineAction('ADD_TODO', [SUCCESS, FAILURE], 'todos');

Yes it's mine. I should have mentioned that. I use it with redux-actions and redux-saga like the example in the readme. And really like the now obvious separation between actions and status updates. While having less boilerplate.

Write a reply...

This comment has received 1 appreciation.

Very nice post @michaelgilley I'm implementing a similar approach you proposed for my PoC.

Write a reply...

This comment has received 1 appreciation.

This all can be done much simpler and more declarative than Redux Saga. You have to look at Cerebral's signals and it's excellent function tree, it can be used not only in Cerebral, but also in Redux or Mobx.

Thanks for the tip @hipertracker. I haven't looked at cerebral yet. I'll have to read more about it and see how it compares.

Write a reply...

Thanks a lot! I've been struggling to maintain a disorganized app for a long time, but I think the concepts you have presented here will do the trick!

Write a reply...

For importing module across features i like Jack hsu ( implementation.

Yes, that scheme is very close to Ducks as well and there are several different ways to stash the modular components of a Redux app. Perhaps the most important is uniformity in design across modules. Also, while I do like the separation of each type into separate files this would also have the disadvantage of even more boilerplate code/structure to manage per module. Thanks for the post!

Write a reply...

Question regarding this statement:

Specifically, we are currently passing every prop needed down from the Container level. These props are either defined in the Container or injected via the connect HoC. While this makes organization simple it also means every change or addition of a prop equates to changes to every JSX component in the tree. This gets tiring.

Do you have some kind of solution for this?

I find myself using one container that is connected to Redux store and passing everything to my children. Result of that is a lot of returning in mapStateToProps and mapDispatchToProps.

One logical solution for me is to separate one big container to maybe 2-3 smaller containers for easier tracking of what is imported and connected.

Is there anything else you can advise?

Thanks! :)

Hi @vblazenka . We use many different containers and at each container level have a separate module. Props are not passed into Containers from parents though (except from Providers like react-intl, Redux, or React-router). It makes for a much flatter architecture than having only 1 container BUT there is still the problem of passing things down multiple levels when components get deep. I'm not sure what the fix for this would be and using context is out of the picture. I just might be the price paid for the other benefits of such a design....

Write a reply...

Thanks for the great post Michael. What if two different modules have common components? How would you organize that?

Thank you @cdharrison. If you have common components (for example forms or a common header) you'd move those out to their own module. We have a few of those global modules like FormElements that don't need a module dir/file but live at the root level anyway and are imported where needed. In these cases the root App module does not import anything from these modules. Great question!

Write a reply...

Great write up. I worked for HPE as a contractor and we had the task of building a marketplace for HPE employees (data scientists). It was a huge application to say the least, we had data lakes, several databases. We used Docker swarms and had micro-services and followed "best practices". Redux was a great way to maintain application state, and helped get the MVP up and running in a few months, which IMO was amazing!

I've looked into MobX as well.

Have you tried Flow instead of TS? I find it cuts down on the boilerplate of TS and is more inline with the way I write JavaScript. Just a suggestion, we all have our preferences.

Write a reply...

thanks for this post! can you point me in the direction of any literature that goes into more depth on the idea of only posting from components — no dispatch? i'd love to learn more.

Yep, you'll want to check out the docs for react-redux here:

In particular, pay attention to mapDispatchToProps, which can take either a function or an object. If you give it an object as a map to your action creators as values these functions will be automatically wrapped in dispatch for you. ;-)

Write a reply...

Great post hugely helpful application-skeleton while developing large applications.

Write a reply...

Join a friendly and inclusive Q&A network for coders

  • 🖥Pick the technologies you like & read great content through your feed.
  • 💬Ask a question when you want to learn more about anything.
  • 🚀Share what you know & build your portfolio.
Sign up nowLearn more

loading ...