Search⌘ K
AI Features

Global State with Zustand

Explore how to manage global state in Next.js using Zustand to improve performance by subscribing components to specific state slices. Learn to create stores, update actions, and reduce unnecessary rerenders for scalable applications.

In our last lesson, we saw how to manage state locally with useState and share it across a component tree with useContext. These are effective tools, but as our application grows, we may notice some scaling challenges. Specifically, any update to the context triggers a rerender in all components that consume it, which can become a performance bottleneck.

While we could build a more robust system with useContext and useReducer, Zustand offers a more direct and performant approach. Its core philosophy is to provide a minimal, unopinionated tool that feels like a natural extension of React.

Why do we need another state manager?

When we build larger applications, we often have “global” state that needs to be accessed by many unrelated components, things like user authentication status, theme preferences, or the contents of a shopping cart.

Zustand’s key advantage is how it handles updates to the global state. It sets up a granular subscription that is only triggered when that specific piece of state changes. This ensures that our components only rerender when necessary, ...