zustand-patterns by yonatangross/orchestkit
npx skills add https://github.com/yonatangross/orchestkit --skill zustand-patterns使用 Zustand 5.x 进行现代状态管理 - 轻量级、TypeScript 优先、无样板代码。
涵盖基础存储、切片、Immer、持久化、选择器、异步操作和开发者工具。
加载 Read("${CLAUDE_SKILL_DIR}/references/core-patterns.md") 以获取所有 7 个核心模式的完整代码示例。
// ✅ 使用双重调用模式创建类型化存储
const useStore = create<State>()((set, get) => ({ ... }));
// ✅ 对所有状态访问使用选择器
const count = useStore((s) => s.count);
// ✅ 对多个值使用 useShallow (Zustand 5.x)
const { a, b } = useStore(useShallow((s) => ({ a: s.a, b: s.b })));
// ✅ 中间件顺序:immer → subscribeWithSelector → devtools → persist
create(persist(devtools(immer((set) => ({ ... })))))
// ❌ 切勿解构整个存储
const store = useStore(); // 任何更改都会导致重新渲染
// ❌ 切勿存储服务器状态(改用 TanStack Query)
const useStore = create((set) => ({ users: [], fetchUsers: async () => ... }));
| 决策 | 选项 A | 选项 B | 推荐 |
|---|
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 状态结构 | 单一存储 | 多个存储 | 单一存储中的切片 - 更易于跨切片访问 |
| 嵌套更新 | 扩展运算符 | Immer 中间件 | Immer 用于深度嵌套状态(3 层以上) |
| 持久化 | 手动 localStorage | persist 中间件 | persist 中间件 配合 partialize |
| 多个值 | 多个选择器 | useShallow | useShallow 用于 2-5 个相关值 |
| 服务器状态 | Zustand | TanStack Query | TanStack Query - Zustand 仅用于客户端状态 |
| 开发者工具 | 始终开启 | 条件开启 | 条件开启 - enabled: process.env.NODE_ENV === 'development' |
禁止的模式(存储解构、派生状态、服务器状态、直接变更)以及 React Query 集成指南。
加载 Read("${CLAUDE_SKILL_DIR}/references/anti-patterns-and-integration.md") 以获取反模式示例和 TanStack Query 分离模式。
tanstack-query-advanced - 服务器状态管理(与 Zustand 配合用于客户端状态)form-state-patterns - 表单状态(React Hook Form 与 Zustand 用于表单)react-server-components-framework - 与 Zustand 配合时的 RSC 水合考量关键词 : zustand, create, store, typescript, state 解决的问题 : 使用正确的 TypeScript 推断设置类型安全的 Zustand 存储
关键词 : slices, modular, split, combine, StateCreator 解决的问题 : 将大型存储组织成可维护的、领域特定的切片
关键词 : immer, persist, devtools, middleware, compose 解决的问题 : 以正确的顺序组合中间件,以实现不可变性、持久化和调试
关键词 : selector, useShallow, re-render, performance, memoization 解决的问题 : 使用正确的选择器模式防止不必要的重新渲染
关键词 : persist, localStorage, sessionStorage, migrate, version 解决的问题 : 在版本之间使用模式迁移持久化状态
使用 Read("${CLAUDE_SKILL_DIR}/references/<file>") 按需加载:
| 文件 | 内容 |
|---|---|
core-patterns.md | 基础存储、切片、Immer、持久化、选择器、异步、开发者工具 |
anti-patterns-and-integration.md | 禁止的模式和 React Query 集成 |
middleware-composition.md | 以正确顺序组合多个中间件 |
其他资源:
Read("${CLAUDE_SKILL_DIR}/scripts/store-template.ts") - 生产就绪的存储模板Read("${CLAUDE_SKILL_DIR}/checklists/zustand-checklist.md") - 实施检查清单每周安装数
128
代码仓库
GitHub 星标数
132
首次出现
2026 年 1 月 22 日
安全审计
安装于
opencode118
gemini-cli118
codex114
github-copilot112
cursor112
claude-code104
Modern state management with Zustand 5.x - lightweight, TypeScript-first, no boilerplate.
Covers basic stores, slices, Immer, persist, selectors, async actions, and devtools.
Load Read("${CLAUDE_SKILL_DIR}/references/core-patterns.md") for full code examples of all 7 core patterns.
// ✅ Create typed store with double-call pattern
const useStore = create<State>()((set, get) => ({ ... }));
// ✅ Use selectors for all state access
const count = useStore((s) => s.count);
// ✅ Use useShallow for multiple values (Zustand 5.x)
const { a, b } = useStore(useShallow((s) => ({ a: s.a, b: s.b })));
// ✅ Middleware order: immer → subscribeWithSelector → devtools → persist
create(persist(devtools(immer((set) => ({ ... })))))
// ❌ Never destructure entire store
const store = useStore(); // Re-renders on ANY change
// ❌ Never store server state (use TanStack Query instead)
const useStore = create((set) => ({ users: [], fetchUsers: async () => ... }));
| Decision | Option A | Option B | Recommendation |
|---|---|---|---|
| State structure | Single store | Multiple stores | Slices in single store - easier cross-slice access |
| Nested updates | Spread operator | Immer middleware | Immer for deeply nested state (3+ levels) |
| Persistence | Manual localStorage | persist middleware | persist middleware with partialize |
| Multiple values | Multiple selectors | useShallow | useShallow for 2-5 related values |
| Server state | Zustand | TanStack Query | TanStack Query - Zustand for client-only state |
| DevTools | Always on | Conditional | Conditional - enabled: process.env.NODE_ENV === 'development' |
Forbidden patterns (store destructuring, derived state, server state, direct mutation) and React Query integration guidance.
Load Read("${CLAUDE_SKILL_DIR}/references/anti-patterns-and-integration.md") for anti-pattern examples and TanStack Query separation patterns.
tanstack-query-advanced - Server state management (use with Zustand for client state)form-state-patterns - Form state (React Hook Form vs Zustand for forms)react-server-components-framework - RSC hydration considerations with ZustandKeywords : zustand, create, store, typescript, state Solves : Setting up type-safe Zustand stores with proper TypeScript inference
Keywords : slices, modular, split, combine, StateCreator Solves : Organizing large stores into maintainable, domain-specific slices
Keywords : immer, persist, devtools, middleware, compose Solves : Combining middleware in correct order for immutability, persistence, and debugging
Keywords : selector, useShallow, re-render, performance, memoization Solves : Preventing unnecessary re-renders with proper selector patterns
Keywords : persist, localStorage, sessionStorage, migrate, version Solves : Persisting state with schema migrations between versions
Load on demand with Read("${CLAUDE_SKILL_DIR}/references/<file>"):
| File | Content |
|---|---|
core-patterns.md | Basic store, slices, Immer, persist, selectors, async, devtools |
anti-patterns-and-integration.md | Forbidden patterns and React Query integration |
middleware-composition.md | Combining multiple middleware in correct order |
Other resources:
Read("${CLAUDE_SKILL_DIR}/scripts/store-template.ts") - Production-ready store templateRead("${CLAUDE_SKILL_DIR}/checklists/zustand-checklist.md") - Implementation checklistWeekly Installs
128
Repository
GitHub Stars
132
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode118
gemini-cli118
codex114
github-copilot112
cursor112
claude-code104
World Monitor 智能仪表板:AI驱动的全球实时监控与地缘风险分析平台
590 周安装