zustand
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity Zustand Best Practices
社区版Zustand最佳实践
Comprehensive performance and architecture guide for Zustand state management in React applications. Contains 43 rules across 8 categories, prioritized by impact from critical (store architecture, selector optimization) to incremental (advanced patterns).
这是一份针对React应用中Zustand状态管理的全面性能与架构指南。包含8个类别共43条规则,按影响优先级排序,从关键级别(store架构、选择器优化)到增量级别(高级模式)。
When to Apply
适用场景
Reference these guidelines when:
- Creating new Zustand stores
- Optimizing re-render performance with selectors
- Implementing persistence or middleware
- Integrating Zustand with SSR/Next.js
- Reviewing code for state management patterns
在以下场景中可参考本指南:
- 创建新的Zustand store
- 使用选择器优化重渲染性能
- 实现持久化或中间件
- 将Zustand与SSR/Next.js集成
- 评审状态管理模式相关代码
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Store Architecture | CRITICAL | |
| 2 | Selector Optimization | CRITICAL | |
| 3 | Re-render Prevention | HIGH | |
| 4 | State Updates | MEDIUM-HIGH | |
| 5 | Middleware Configuration | MEDIUM | |
| 6 | SSR and Hydration | MEDIUM | |
| 7 | TypeScript Patterns | LOW-MEDIUM | |
| 8 | Advanced Patterns | LOW | |
| 优先级 | 类别 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | Store架构 | 关键 | |
| 2 | 选择器优化 | 关键 | |
| 3 | 重渲染预防 | 高 | |
| 4 | 状态更新 | 中高 | |
| 5 | 中间件配置 | 中 | |
| 6 | SSR与水合 | 中 | |
| 7 | TypeScript模式 | 中低 | |
| 8 | 高级模式 | 低 | |
Quick Reference
快速参考
1. Store Architecture (CRITICAL)
1. Store架构(关键)
- - Use multiple small stores instead of one monolithic store
store-multiple-stores - - Separate actions from state in dedicated namespace
store-separate-actions - - Name actions as events not setters
store-event-naming - - Colocate actions with the state they modify
store-colocate-logic - - Derive computed values instead of storing them
store-avoid-derived-state - - Organize stores by feature domain
store-domain-boundaries
- - 使用多个小型store替代单一大型store
store-multiple-stores - - 在独立命名空间中将操作与状态分离
store-separate-actions - - 操作命名采用事件而非设置器形式
store-event-naming - - 将操作与它们修改的状态放在一起
store-colocate-logic - - 推导计算值而非直接存储
store-avoid-derived-state - - 按功能领域组织store
store-domain-boundaries
2. Selector Optimization (CRITICAL)
2. 选择器优化(关键)
- - Always use selectors never subscribe to entire store
select-always-use - - Use atomic selectors for single values
select-atomic-picks - - Ensure selectors return stable references
select-stable-returns - - Export custom hooks not raw store
select-custom-hooks - - Use auto-generated selectors for large stores
select-auto-generate - - Memoize expensive computed selectors
select-memoize-computed - - Define selectors outside components
select-avoid-inline
- - 始终使用选择器,绝不订阅整个store
select-always-use - - 对单一值使用原子选择器
select-atomic-picks - - 确保选择器返回稳定的引用
select-stable-returns - - 导出自定义钩子而非原始store
select-custom-hooks - - 对大型store使用自动生成的选择器
select-auto-generate - - 对开销大的计算选择器进行记忆化
select-memoize-computed - - 在组件外部定义选择器
select-avoid-inline
3. Re-render Prevention (HIGH)
3. 重渲染预防(高)
- - Use useShallow for multi-property selections
render-use-shallow - - Provide custom equality functions when needed
render-equality-fn - - Memo children affected by parent store updates
render-memo-children - - Use subscribe for non-React consumers
render-subscribe-external - - Avoid returning new objects from selectors
render-avoid-object-returns - - Split components to minimize subscription scope
render-split-components
- - 对多属性选择使用useShallow
render-use-shallow - - 必要时提供自定义相等性函数
render-equality-fn - - 对受父store更新影响的子组件进行记忆化
render-memo-children - - 对非React消费者使用subscribe
render-subscribe-external - - 避免从选择器返回新对象
render-avoid-object-returns - - 拆分组件以最小化订阅范围
render-split-components
4. State Updates (MEDIUM-HIGH)
4. 状态更新(中高)
- - Use functional form when updating based on previous state
update-functional-set - - Never mutate state directly
update-immutable - - Understand set() shallow merge behavior
update-shallow-merge - - Handle async actions with loading and error states
update-async-actions - - Batch related updates in single set call
update-batch-updates
- - 基于之前状态更新时使用函数式形式
update-functional-set - - 绝不直接修改状态
update-immutable - - 理解set()的浅合并行为
update-shallow-merge - - 处理异步操作时包含加载和错误状态
update-async-actions - - 在单个set调用中批量处理相关更新
update-batch-updates
5. Middleware Configuration (MEDIUM)
5. 中间件配置(中)
- - Name actions for DevTools debugging
mw-devtools-actions - - Use partialize for selective persistence
mw-persist-partialize - - Version and migrate persisted state
mw-persist-migration - - Use immer for deeply nested state updates
mw-immer-nested - - Apply middlewares in correct order
mw-combine-order - - Apply middleware at combined store level
mw-slice-middleware
- - 为DevTools调试给操作命名
mw-devtools-actions - - 使用partialize实现选择性持久化
mw-persist-partialize - - 对持久化状态进行版本控制和迁移
mw-persist-migration - - 使用Immer处理深度嵌套的状态更新
mw-immer-nested - - 按正确顺序应用中间件
mw-combine-order - - 在组合store级别应用中间件
mw-slice-middleware
6. SSR and Hydration (MEDIUM)
6. SSR与水合(中)
- - Use skipHydration in SSR contexts
ssr-skip-hydration - - Manually rehydrate on client mount
ssr-manual-rehydrate - - Use custom hook to prevent hydration mismatch
ssr-hydration-hook - - Guard browser APIs with typeof window check
ssr-check-window
- - 在SSR环境中使用skipHydration
ssr-skip-hydration - - 在客户端挂载时手动进行水合
ssr-manual-rehydrate - - 使用自定义钩子避免水合不匹配
ssr-hydration-hook - - 用typeof window检查保护浏览器API
ssr-check-window
7. TypeScript Patterns (LOW-MEDIUM)
7. TypeScript模式(中低)
- - Use StateCreator for slice typing
ts-state-creator - - Preserve type inference with middleware
ts-middleware-inference - - Separate state and actions interfaces
ts-separate-types - - Type selectors for reusability
ts-generic-selectors - - Type combined stores correctly
ts-bound-store
- - 使用StateCreator进行切片类型定义
ts-state-creator - - 保留中间件的类型推断
ts-middleware-inference - - 分离状态和操作的接口
ts-separate-types - - 为选择器添加类型以提升复用性
ts-generic-selectors - - 正确为组合store添加类型
ts-bound-store
8. Advanced Patterns (LOW)
8. 高级模式(低)
- - Combine Zustand with React Context for dependency injection
adv-context-stores - - Use subscribe for transient updates
adv-transient-updates - - Implement computed state with getters
adv-computed-getters - - Integrate with React Query and SWR
adv-third-party-integration
- - 将Zustand与React Context结合实现依赖注入
adv-context-stores - - 使用subscribe处理临时更新
adv-transient-updates - - 使用getter实现计算状态
adv-computed-getters - - 与React Query和SWR集成
adv-third-party-integration
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |