Zustand is a lightweight, fast, and scalable state-management solution for React, built on simplified flux principles. It offers a comfortable API based on hooks, designed to be unopinionated and address common React pitfalls like the zombie child problem. The current stable version is 5.0.12, with active development and frequent patch releases.
npm install zustandVerified import paths — ran on the pinned version, not inferred.
This quickstart defines a simple Zustand store, `useBearStore`, with initial state and actions. It then demonstrates how to consume and update this state from two separate React components without needing a Provider.
Always return a new state object from `set` callbacks (e.g., `set((state) => ({ ...state, property: newValue }))`) or provide a new object when calling `set`.Select only the specific state slices your component needs (e.g., `useBearStore((state) => state.bears)`). For multiple non-primitive slices, consider `shallow` from `zustand/shallow` or a custom equality function.
For non-primitive return values from selectors (e.g., `useBearStore((state) => ({ count: state.bears, name: state.name }))`), use `shallow` from `zustand/shallow` as a second argument to `useBearStore` (e.g., `useBearStore(selector, shallow)`).Manage all persistent state exclusively through Zustand's `persist` middleware and `set` function, avoiding direct interaction with the storage API.
Ensure you are using `import { create } from 'zustand'` and your build system is configured for ESM.Ensure all Zustand store hooks are invoked exclusively within the top level of React function components or custom hooks.
Always ensure `set` callbacks return a complete (or new partial) state object. For example, `set((state) => ({ ...state, propertyName: newValue }))`.No dependency data recorded yet.