Zustand — Bear-Sized State Management for React
Zustand is a small, fast, and scalable bearbones state-management solution for React. A cozy alternative to Redux with a simple hooks API, no providers needed, and first-class TypeScript support.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 2f0e010c-3567-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
What it is
Zustand is a lightweight state management library for React. It uses a simple hooks-based API, requires no context providers, and supports TypeScript out of the box. At under 1KB gzipped, it is one of the smallest state management solutions available.
Zustand targets React developers who want global state without the boilerplate of Redux or the complexity of context-based solutions.
How it saves time or tokens
Zustand eliminates provider wrappers, action creators, reducers, and dispatch patterns. You define a store in one function call and consume it with a hook. No selectors library, no middleware setup, no normalized state shape required.
How to use
- Install Zustand:
npm i zustand
- Create a store:
import { create } from 'zustand';
type BearStore = {
bears: number;
increase: () => void;
reset: () => void;
};
export const useBearStore = create<BearStore>((set) => ({
bears: 0,
increase: () => set((s) => ({ bears: s.bears + 1 })),
reset: () => set({ bears: 0 }),
}));
- Use in any component — no provider needed:
function Counter() {
const bears = useBearStore((s) => s.bears);
const increase = useBearStore((s) => s.increase);
return <button onClick={increase}>{bears} bears</button>;
}
Example
import { create } from 'zustand';
interface TodoStore {
todos: string[];
add: (todo: string) => void;
clear: () => void;
}
const useTodoStore = create<TodoStore>((set) => ({
todos: [],
add: (todo) => set((s) => ({ todos: [...s.todos, todo] })),
clear: () => set({ todos: [] }),
}));
// Use anywhere without wrapping in a Provider
function TodoList() {
const todos = useTodoStore((s) => s.todos);
return <ul>{todos.map((t) => <li key={t}>{t}</li>)}</ul>;
}
Related on TokRepo
- AI Tools for Coding — Frontend development tools and libraries
- Featured Workflows — Discover trending React tools
Key considerations
When evaluating Zustand for your workflow, consider the following factors. First, assess whether your team has the technical prerequisites to adopt this tool effectively. Second, evaluate the maintenance burden against the productivity gains. Third, check community activity and documentation quality to ensure long-term viability. Integration with your existing toolchain matters more than feature count alone. Start with a small pilot project before rolling out across the organization. Monitor resource usage during the initial adoption phase to identify bottlenecks early. Document your configuration decisions so team members can onboard independently.
Common pitfalls
- Selecting the entire store object (no selector function) causes unnecessary re-renders; always use selector functions.
- Zustand stores are singletons; in SSR frameworks like Next.js, use per-request store creation to avoid data leaking between users.
- Middleware (persist, devtools) must be applied at store creation time; they cannot be added later.
常见问题
Zustand is much simpler. No action types, reducers, or dispatch. You define state and actions in one function. Redux offers more structure and middleware ecosystem for large applications. Zustand is better for small to medium state needs.
Yes. Zustand works with Next.js, but SSR requires care. Create stores per-request to avoid sharing state between users. The Zustand documentation covers Next.js patterns specifically.
Yes. Zustand has first-class TypeScript support. Define your store type and Zustand infers selector return types, action parameters, and state shape automatically.
Yes. Zustand's persist middleware saves state to localStorage, sessionStorage, or any custom storage adapter. State is automatically rehydrated when the app loads.
Zustand is under 1KB gzipped. It has no dependencies beyond React. This makes it one of the smallest state management libraries available for React.
引用来源 (3)
- Zustand GitHub— Small, fast state management for React with hooks API
- Zustand Demo— No providers needed, under 1KB gzipped
- Zustand Documentation— First-class TypeScript support and persist middleware
讨论
相关资产
Valtio — Proxy-Based State Management for React
Valtio makes React state management simple by wrapping plain JavaScript objects in a proxy, so components automatically re-render when the properties they read change — no reducers, actions, or boilerplate required.
Jotai — Primitive and Flexible State Management for React
Jotai is an atomic state library for React. You compose atoms like React useState, and Jotai handles subscription, derivation, and async loading — all without the boilerplate of Redux or the global store of Zustand.
Immer — Immutable State with Mutable Syntax
Immer lets you create the next immutable state by mutating the current one. Write mutating code as if it were normal JS and Immer produces a new immutable object via structural sharing. Built into Redux Toolkit and Zustand.
Nx — Smart Monorepos and Fast CI for JavaScript and Beyond
Nx is a build system and monorepo tool with first-class support for JavaScript, TypeScript, Go, Python, Java, and more. It offers computation caching, affected commands, task orchestration, and distributed execution — scaling from small repos to Google-sized codebases.