Zustand MIT-licensed state-management library with hook-based React stores, selectors and optional persistence middleware.
Overview Zustand is an open-source state-management library with a hook-based React API. A store contains values and update actions, and components subscribe to selected state. Optional middleware adds persistence. The compact interface can suit custom application state, but it does not remove the need to design immutable updates, server-rendering boundaries or stored-data migrations.[2] [5]
Contents Store and subscription workflow A Zustand store can contain state and the actions that update it. React components select the portions they need through a hook, without a provider in the simple client-only pattern. Selecting the entire store broadens the component's subscription. This makes the API concise, but the application still decides ownership, update boundaries and which state belongs outside individual components.[2]
Shallow merging is not deep mutation support The set function merges at one level. Updating a nested object requires retaining its other fields explicitly, rather than assuming the store recursively merges them. A replacement flag disables merging entirely. For generated code, this distinction is important: a short nested update can discard neighboring fields even though the top-level update appears reasonable. The documented state model remains immutable.[3]
Version 5 selector requirements The v5 migration guide raises the React minimum to 18 and changes equality handling. Selectors that return fresh references can cause update loops, with useShallow or stable fallback values offered as remedies. Custom equality functions use the separate traditional APIs rather than the basic create function. These are behavioral migration requirements, not just import renames.[4]
Persist only the intended data Persistence middleware supports several storage backends, field selection through partialize and explicit version migrations. Its default merge is shallow, which can lose newer nested defaults when older partially persisted objects are restored. Asynchronous storage also hydrates after initial rendering, so a default value is not proof that there is no saved value. The application must distinguish loading persisted state from the settled state and decide what data should be retained.[5]
Server rendering changes the simple-store pattern The Next.js guide warns that module-global stores can be shared across requests and requires a per-request store. It also calls for matching server/client initial data and says React Server Components should not read or write the store. The page notes that further guidance is being developed, so this article retains the explicit isolation boundaries without claiming a universal framework recipe.[6]
License and substitution Zustand is MIT-licensed. It can replace Redux Toolkit for appropriate shared client-state tasks where its store and selector model fits. Redux reducers, middleware and optional server-data caching are separate migration concerns. No measured bundle advantage, performance ranking or hands-on migration is claimed.[1] [2] [7]
Choosing an object store instead of atoms Zustand can substitute for Jotai for shared React client state when an application is easier to organize around an object containing values and update actions. Components can subscribe with selectors rather than composing primitive and derived atoms. This can suit a compact module-level store with explicit operations, while Jotai's atom model may be more natural for independently composed state. Moving an existing application requires translating atom reads, derived values and writes into store logic. Preserve any separate subtree lifetimes deliberately: replacing several scoped Jotai Providers with one global store would change which component instances share state.[8] [9] [10]
Also included: 5 capabilities and 0 substitute relationships. Check substitutes in section 1 and capabilities in section 3.