zustand
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseZustand State Management
Zustand 状态管理
Overview
概述
Zustand v5 is a lightweight global state manager for React built on . Requires React 18+ (uses internally). When using , install as a peer dependency. It provides type-safe stores, atomic selectors for minimal re-renders, composable middleware (persist, devtools, immer, subscribeWithSelector), and a slices pattern for large applications. Use Zustand for client-only global state; use TanStack Query for server-fetched data.
useSyncExternalStoreuseSyncExternalStorecreateWithEqualityFnuse-sync-external-storeWhen to use: Client-side global state, persistent user preferences, complex multi-domain stores, cross-component state sharing, vanilla (non-React) state management.
When NOT to use: Server state with caching needs (use TanStack Query), single-component state (use useState), simple prop drilling scenarios.
Zustand v5是一款基于的轻量级React全局状态管理器,要求React 18及以上版本(内部使用)。使用时,需要安装作为对等依赖。它提供类型安全的存储、最小化重渲染的原子选择器、可组合的中间件(persist、devtools、immer、subscribeWithSelector),以及面向大型应用的切片模式。Zustand适用于客户端专属的全局状态管理;服务器端获取的数据请使用TanStack Query。
useSyncExternalStoreuseSyncExternalStorecreateWithEqualityFnuse-sync-external-store适用场景: 客户端全局状态、持久化用户偏好、复杂多域存储、跨组件状态共享、vanilla(非React)状态管理。
不适用场景: 需要缓存的服务器状态(使用TanStack Query)、单一组件状态(使用useState)、简单的属性透传场景。
Quick Reference
快速参考
| Pattern | API | Key Points |
|---|---|---|
| Basic store | | Double parentheses required for TS |
| Atomic selector | | Only re-renders when selected value changes |
| Multiple values | | Import from |
| Persist | | localStorage with SSR hydration handling |
| Devtools | | Redux DevTools integration |
| SubscribeWithSelector | | Subscribe to state slices outside React |
| Middleware order | | Outer to inner wrapping |
| Slices pattern | | Split store by domain |
| SSR provider | | Per-request stores prevent data leaks |
| Immer | | Safe nested state mutations |
| Vanilla store | | Use outside React |
| Reset store | | Use |
| Derived values | | Compute in selector |
| Auto selectors | | Generate |
| 模式 | API | 核心要点 |
|---|---|---|
| 基础存储 | | TypeScript下必须使用双括号 |
| 原子选择器 | | 仅当选中值变化时才会重渲染 |
| 多值选择 | | 从 |
| 持久化 | | 结合localStorage并支持SSR hydration处理 |
| 开发者工具 | | 集成Redux DevTools |
| SubscribeWithSelector | | 在React外部订阅状态切片 |
| 中间件顺序 | | 从外到内包裹 |
| 切片模式 | | 按领域拆分存储 |
| SSR提供者 | | 每个请求独立存储,避免数据泄露 |
| Immer中间件 | | 安全的嵌套状态突变 |
| Vanilla存储 | | 用于非React场景 |
| 重置存储 | | 使用 |
| 派生值 | | 在选择器中计算 |
| 自动生成选择器 | | 自动生成 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Using | Use |
Selecting full state object with | Use atomic selectors like |
Importing | Import from |
| Creating new object references in selectors causing infinite loops | Use separate primitive selectors or wrap with |
| Using a global singleton store with SSR or Next.js | Use the |
| Tracking initial state manually for reset | Use |
| Using Zustand for server-fetched data that needs caching and revalidation | Use TanStack Query for server state; reserve Zustand for client-only global state |
| 错误 | 正确用法 |
|---|---|
使用单括号的 | TypeScript下请使用 |
使用 | 使用原子选择器如 |
从 | 请从 |
| 在选择器中创建新对象引用导致无限循环 | 使用独立的原始类型选择器,或用 |
| 在SSR或Next.js中使用全局单例存储 | 使用 |
| 手动跟踪初始状态以实现重置 | 使用Zustand自动提供的 |
| 使用Zustand管理需要缓存和重新验证的服务器端获取数据 | 服务器状态请使用TanStack Query;Zustand仅用于客户端专属全局状态 |
Delegation
任务分工
- Store architecture and slices design: Use agent to define domain boundaries, slice interfaces, and middleware composition order
Plan - Hydration and SSR debugging: Use agent to diagnose persist middleware issues, hydration mismatches, and Next.js provider setup
Task - Migration from v4 to v5: Use agent to find deprecated import paths, single-parentheses patterns, and legacy middleware usage
Explore - Testing strategy: Use agent to set up store mocks and test isolation patterns
Task
- 存储架构与切片设计:使用Plan agent定义领域边界、切片接口和中间件组合顺序
- Hydration与SSR调试:使用Task agent排查persist中间件问题、hydration不匹配及Next.js提供者配置问题
- 从v4迁移到v5:使用Explore agent查找已废弃的导入路径、单括号模式及旧版中间件用法
- 测试策略:使用Task agent设置存储模拟及测试隔离模式
References
参考资料
- Store patterns, TypeScript, and core API
- Middleware: persist, devtools, immer, subscribeWithSelector, and custom
- Slices pattern and large store architecture
- SSR, Next.js, and hydration handling
- Testing stores with Vitest and Jest
- Migration guide: Redux, Context, and v4 to v5
- 存储模式、TypeScript及核心API
- 中间件:persist、devtools、immer、subscribeWithSelector及自定义中间件
- 切片模式与大型存储架构
- SSR、Next.js及hydration处理
- 使用Vitest和Jest测试存储
- 迁移指南:从Redux、Context及v4到v5