DEV Community

Discussion on: Do you need a State Management Library?

Collapse
 
krtirtho profile image
Kingkor Roy Tirtho

If you're using react-query/react-swr for data fetching/caching then Context API will be enough for managing the simple UI states

Collapse
 
ivan_jrmc profile image
Ivan Jeremic • Edited

I use also react-query and also sometimes context for UI state but mostly recoil, what are you doing in context to avoid rerenders or do you simply don't care if components rerender?

Collapse
 
krtirtho profile image
Kingkor Roy Tirtho

I use zustand in this case. It's the most efficient in this case

Thread Thread
 
ivan_jrmc profile image
Ivan Jeremic • Edited

Hard to use zustand when the creator of it himself says Jotai is better. They are both from the same team and Jotai is inspired by recoil.

Thread Thread
 
krtirtho profile image
Kingkor Roy Tirtho

What? When? I never saw him writing such thing. Not even on the README. BTW, zustand has double stars in Github compared to jotai

Also, zustand has over 113k downloads per week in npmjs.com where jotai is only has 5-7k downloads per week

Thread Thread
 
ivan_jrmc profile image
Ivan Jeremic

It was a tweet.

Collapse
 
link2twenty profile image
Andrew Bone

What are you doing in context to avoid rerenders?

Good question, it is my understanding that a component will only rerender if the context in question is loaded in using useContext. Because of this I find it best to have several contexts for different types of data and only add the contexts where they are relevant.

For instance I may have a user data context that gets data from the server but then I may also have a permissions context that gets different data from the server but won't need to update if the user updates their profile picture.

By keeping contexts separate, and using them sparingly, you can prevent excessive redraws.

Thread Thread
 
ivan_jrmc profile image
Ivan Jeremic • Edited

Yes that is one way creating multiple contexts and making sure each context keeps only one piece of state never more than one, to avoid a bunch of code and boilerplate I recommend you to look at recoil it is just react under the hood no external deps. It does the same but without you need to take care how and where to inject the context

Collapse
 
link2twenty profile image
Andrew Bone

I tend to use vanilla fetch for my queries rather than a hook, though I do sometimes make a hook like my example one to handle the initial fetch and refreshes. Are there any advantages to using react query other than it makes initial setup time quicker?

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

Yes manny, mostly boilerplate you need to much boilerplate if you do everything yourself, I know it is not hard but it simply is more code.