Search code examples
reactjsreduxnext.jsredux-toolkitreact-context

What to use as global state manager in React NextJS?


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.


Solution

  • 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.