I am using NextJS and would like to know what is the best global state manager to use. ContextAPI or Redux - Toolkit. Looking for an answer in the Internet, I got the impression that ContextAPI is used more often. But for some reason I'm not sure if this is the best solution. ContextAPI would be a good solution if it have not so many rerenders.
Asked
Active
Viewed 679 times
1
-
You can also try using https://recoiljs.org/. Its hook-based and performance is also good. – Shubham J. Jul 22 '21 at 10:35
-
We cant decide this for you. Each has pros and cons. This is opinion based. – zero298 Jul 22 '21 at 10:41
1 Answers
3
You can look into Redux or any other state manager, but no matter what blog articles try to tell you, Context is not suited for state management.
Alternatives to Redux would be MobX, Recoil, XState, Valtio or Zustand, all giving you a different mental model to use.
Context on the other hand is not suited for state handling as it requires a lot of manual optimizations and even then has performance problems that cannot be solved in current React. Take a look at https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/#context-and-rendering-behavior for an in-depth explanation.

phry
- 35,762
- 5
- 67
- 81
-
When it comes to regular React, it's simple; when it comes to NextJS(SSR), using Redux gets a little more complicated. Perhaps this is the reason that Redux is used less in NextJS. Its easy to find source code with React+Redux-toolkit but harder with NextJS+Redux-toolkit. Here is an example just with using contex without Redux https://github.com/offsecdev/PickBazar – Gregory Kafanov Jul 22 '21 at 11:45
-
@GregoryKafanov still leaves 5 more libraries I added up there that are not context - which is - repeating myself - *not* suited for application state. Also, the challenges with state management in general an NextJS appear only in SSR scenarios and every tool - including Redux and Context (if you count that) will face the exact same problems here. – phry Jul 22 '21 at 12:31