r/reactjs • u/voltomper • Jul 22 '22
What react state management tools do you guys use?
I found this article on Medium where it stated a few state management tools. Until now I was using Redux, but I'm thinking of changing. I was curious on what do you guys use and why?
link to article:
https://medium.com/@pitis.radu/react-state-management-in-2022-345c87922479
54
Upvotes
113
u/Roguewind Jul 22 '22
Whatever you do, don’t change state management tools because of an article on Medium. Even if the article is written by someone who actually knows what they’re talking about, they’re almost all based around getting clicks. So they inevitably take the position that the new hotness > old reliable. Then 6 months later there’s a new new hotness.
Redux is still in use because it’s been stable over a long period of time. So if you plan on moving away from it, make sure you’re not just jumping on a bandwagon that has a few new features (that you might not really need)that redux will implement soon anyway.
15
u/meles984 Jul 22 '22
Mobx at work and Zustand for smaller/side projects
4
u/franciscopresencia Jul 23 '22 edited Jul 23 '22
I made my own 1kb state management library!
https://statux.dev/
The main function
useStore()behaves like React'suseState(), and has a bunch of useful helpers:7
u/DamienNF Jul 22 '22
Do you use Redux Toolkit?
5
u/stoneblade12 Jul 22 '22
We use redux mixed with context but we’re trying to move away from redux
50
u/Tavi2k Jul 22 '22
React Query for server-side state, for anything else just plain old React useState/useReducer. For some rarely changing state like the current user and session details React Context as well.
A lot of state can be local, which makes components more reusable and reduces complexity. Plain old React state management is an entirely valid choice for a lot of state. Of course there are exceptions, there are no universal answers to this question.
9
u/Cautious_Variation_5 Jul 22 '22
I like Zustand + SWR because I work mainly on small projects so I don't need something much fancier than that. Besides doing a great job, those libs are very lightweight as well.
8
u/steveox152 Jul 22 '22
My advice to my developers has always been that we shouldn’t try to solve a problem we don’t have.
If you already have something implemented, I wouldn’t change it unless it solves a problem that you are having.
Redux has its use cases and it’s popular for a reason, Redux Toolkit makes it much more manageable. If I was creating a new project today, I wouldn’t reach for a state management tool until I really needed it, most state is server side and should remain that way if possible.
Overall I have enjoyed my experiences with Redux Toolkit and Zustand. I would most likely reach for Zustand in smaller projects with simple state. Ultimately I would end up using whatever the team is comfortable with on a work project ( within reason ), that is much more valuable.
As another commenter said, you need to evaluate your state problems and see how each tool solves those problems, or creates new problems.
14
u/Thisisvexx Jul 22 '22
Recoil all the way. Smooth and easy to use
6
u/LessSwim Jul 23 '22
React Query + XState
1
u/smettboi Jul 23 '22
I love easy-peasy. Simplified wrapper for redux.
2
u/FRIKI-DIKI-TIKI Jul 23 '22 edited Jul 23 '22
I use xState, all of these ad-hoc solutions are due to people not understanding automata and the problem of state spaces, as well the problem domain they are trying to solve. So many of them implement portions of a state machine and usually do it poorly because they do not understand that there is a full solution to modeling the control flow timing of an application and how it can change state over time. So they scratch an itch of a pain point that they feel not realizing that application time, change and entropy (bugs) are directly related.
If we had a decent well supported option for Behavior Trees in the JS world I would probably use that because Hierarchal Finite State Machines have some advantages in flexibility and Behavioral Tree provide a decent model for HFSM's. xState does support HFSM but not in the way a true behavioral tree implementation does.
3
u/True_Scorpio23 Jul 22 '22
Checkout Mark Eriksons blog, he’s one of the people that maintain the redux project. He has some interesting articles about the history of state management and redux, although its obvsiouly oriented towards react and redux, as someone who works exclusively with those tools I find their history interesting as well.
One other interesting video I found was from the creator of react-query, forget his first name but his last name is Tinsley. The video I’m talking about is at some conference where he goes over the reasons for creating it. Something I had not learned or considered was how in a production enterprise application you have your client state and server state.
The big takeaway from these two sources and the point I’m trying to make is that all of these are just tools. Depending on the use case, a combination of the available tools may work better than a different combination for a separate use case. But it is helpful to know about these and their trade offs so that you can make a wise decision in the future, should you be in that position to do so.
3
u/saito200 Jul 22 '22
- Redux toolkit for global app state
- react-query for server state
- url path params for some things such as an uid to fetch data from the server (handled with react-router)
- For local component state, just useState, or useReducer if the state is complex
- For form state, actually used mantine forms. If my UI library wouldn't handle that, I would use react-form-hooks. Yup for schema validation
2
u/_Pho_ Jul 22 '22
Zustand if I need it which I rarely do
1
u/vanravan Jul 22 '22
We are using redux and react context. Project was started in 2016 and was using redux since then. After release of react context we started to rely less on redux and don't extend the redux state that often.
We haven't taken the time to migrate to redux toolkit yet
1
u/fzammetti Jul 22 '22
Hookstate. Not enough love for that library, to be honest. A little annoying having to get() and put() things sometimes, but it's consistent, and just generally very simple. I like simple. Harder to fuck up simple.
1
u/simlmx Jul 22 '22
Redux alongside redux-saga. I'm all for simplifying it up but I haven't found an alternative that felt like enough of a game changer to justify learning it.
1
u/andoy Jul 23 '22
RTK. it is so easy to use.
1
u/slaymaker1907 Jul 23 '22
I like still really like Redux. It's very simple, but it imposes a lot of good structure on projects. In particular, I like the emphasis on large actions resulting in less weird in-between states like I saw with setter/getter/listener based systems. I also like the single, consistent global state instead of trying to compartmentalize too much.
1
u/hwasung Jul 23 '22
Our current spaghetti code base is being migrated to react from angular - all of the needs that we have had thus far have been addressed by judicious use of useState, useContext, and custom built hooks with liberal React Query as needed. Super pleased with the current state of state management products in React 18 and have been able to get everything we need done.
1
u/cayter Jul 23 '22
If you would like to get away from all these complexities and major use case is form submission, go Remix. Speaking from my experience for introducing and running React SPA w/ webpack 2/3 to Grab from year 2015 to 2017.
1
u/[deleted] Jul 23 '22 edited Jul 23 '22
React Beta Docs are doing well to address how to handle state management appropriately.
https://beta.reactjs.org/learn/managing-state
tldr: Lift state up as you go. Up to Context/useReducer.
Don't use React for data coming over the network. Use a cache invalidation compatible library like React Query.
You should be covered in almost every case with this.
If for whatever reason you need local, global state, Context/usereducer around your app. Do NOT start with global context. Only lift state up as high as it absolutely needs to be.
And keep contexts small and atomic. Don't load them up with too much stuff.
To prevent rerendering of use context, pass props to a view only component (use a parent component to get data from context, pass specific properties into a child component) or Memoize the return value of the component.
1
u/JonnyBoy89 Jul 23 '22
I’ve been wanting to play around with x state too. It’s not on their list, but it uses the concept of state machines and state maps. Kinda cool
1
u/listrome1 Jul 23 '22
I recently started using redux toolkit. It really my help with global state management. Otherwise I just use regular state and props for local state management.
1
u/CommandLionInterface Jul 23 '22
Redux with redux toolkit. If it ain’t broken…
1
u/frosty_lupus Jul 23 '22
Remix
1
u/totalolage Jul 23 '22 edited Jul 23 '22
Zustand with immer + TRPC
Zustand is so great, it works just as expected, I have yet to run into any unexpected behaviour (wish I could say the same about react itself).
TRPC encourages putting request logic on the serverside, which is a necessary pattern for performance on lower end devices. It also uses react-query under the hood so all the niceities of that are included by default.
I tried Jotai, but I didn't manage to figure out a pattern that would be a notable improvement for more complex states. For example I have an app where you can place, drag, and resize signatures on a pdf. The state for that is necessarily quite complex, since boundaries need to be checked, scaling accounted for etc. and Zustand works great. Jotai has a weird pattern around every last thing having to be updated explicitly with a
setcall which starts cluttering pretty fast.1
u/RyanNerd Jul 23 '22
ReactN easy intuitive and uses hooks in a logical way.
1
u/Rocket-Shot Feb 14 '23
webKrafters/react-observable-context. Check this out on npm. It is a react context extension that serves as a state manager for component trees or for the whole UI. But without the react-context drawbacks.
0
u/yourgirl696969 Jul 22 '22
None lol context is just so much better for me. Mix it with useReducer and you pretty much already have redux out of the box.
1
u/med8bra Jul 22 '22
You don't need it, server-side state management with some data normalization features on frontend (react query, graphql apollo) is enough
For complex component state management, react useReducer does the job
Also, for state that's required in multiple places (deep children), react Context API is a good pattern (auth)
Finally, you can do Redux the right way, but it's always a mess after some months of development.
-2
u/[deleted] Jul 22 '22
Redux is the only tool there. No ?
-2
u/bondoli Jul 22 '22
React query is trash, I like recoil but it doesn't work well with ssr so I'm switching back to redux/redux-toolkit. RQ and recoil are also not the easiest to test, which is important.
0
u/[deleted] Jul 22 '22
[deleted] — view deleted comment