Ask HN: What are the best alternatives to MobX for front-end state management?
I left Redux camp the year it was created due to high complexity of apps created with it, and switched to MobX which employs more mutable/reactive approach, which I found quite fast to use and surprisingly manageable.
I wonder if there exist now better alternatives? What do you recommend?
6 comments
[ 1.7 ms ] story [ 22.1 ms ] threadYou can wrap your whole app in a context provider, and/or combine it with state trees that only certain features or components or users need. Then any child within any tree can pull from the contexts.
It's up to you whether you want to use a reducer like pattern, or make the setter itself available in the context so any consumer can mutate state, or pass along a helper function that sets state as an abstraction.
Any way you do it, it will be far easier to use than the nightmare that is Redux. It might be more disorganized, with a lot of separate state hooks, but each one can be relatively modular. Overall I found it waaaaaay easier than Redux.
I strongly recommend reading through the "Redux Essentials" tutorial [0] in the Redux docs, which has been specifically designed to show "how to use Redux, the right way" with our recommended practices
The older patterns shown in almost all other tutorials on the internet are still valid, but not how we recommend writing Redux code today.
You should also read through the Redux "Style Guide" docs page, which explains our recommended patterns and best practices. Following those will result in better and more maintainable Redux apps.
Redux isn't the right tool for all use cases, and we don't expect everyone to like Redux :) But writing Redux code today with Redux Toolkit and React-Redux hooks is drastically simpler than the legacy-style patterns were a few years ago, and we routinely get positive feedback from folks who hated Redux before and now love using RTK.
It's also worth noting that there's significant technical differences in how Context + `useReducer` work vs how (React-)Redux works, and that affects what kinds of tasks they're suitable for. I covered those in extensive detail in a post [2].
[0] https://redux.js.org/tutorials/essentials/part-1-overview-co...
[1] https://redux.js.org/style-guide/
[2] https://blog.isquaredsoftware.com/2021/01/context-redux-diff...
I will definitely check out your links when I get back to work next week. Thank you for sharing!
Also see these talks I did on "modern Redux with RTK" and "the evolution of Redux async logic":
- https://blog.isquaredsoftware.com/2022/06/presentations-mode...
- https://blog.isquaredsoftware.com/2022/05/presentations-evol...
https://keajs.org/