zustand-patterns
yonatangross/orchestkit
Reference for Zustand 5.x state management patterns with TypeScript, middleware, and persistence.
What is zustand-patterns?
Zustand Patterns is a reference guide for building type-safe, lightweight state management in React using Zustand 5.x. It documents 7 core patterns including slices, middleware composition, Immer integration, selectors, and persistence—use it when implementing client-side state management as an alternative to Redux.
- Provides TypeScript-first patterns for creating and typing Zustand stores with proper inference
- Documents the slices pattern for organizing large stores into modular, domain-specific pieces
- Guides middleware composition order (immer → subscribeWithSelector → devtools → persist)
- Explains selector optimization and useShallow for preventing unnecessary re-renders
- Covers persistence with localStorage/sessionStorage, schema versioning, and migrations
- Identifies anti-patterns (store destructuring, derived state, server state in Zustand)
How to install zustand-patterns
npx skills add https://github.com/yonatangross/orchestkit --skill zustand-patterns- Zustand >=5.0.0 installed in your project
- React 18+ (v5 floor)
- Basic familiarity with React hooks and TypeScript
How to use zustand-patterns
- 1.Review the Quick Reference section to understand the double-call pattern and selector basics
- 2.Load the store-template.ts script to see a production-ready example
- 3.Decide on store structure: single store with slices vs. multiple stores (slices recommended)
- 4.Choose middleware stack based on needs: immer for nested updates, persist for localStorage, devtools for debugging
- 5.Implement selectors for all state access; use useShallow for 2-5 related values
- 6.Load anti-patterns-and-integration.md to identify what NOT to do and how to integrate with TanStack Query
- 7.Use the zustand-checklist.md to verify your implementation before shipping
Use cases
- Setting up a new Zustand store with TypeScript and middleware stack for a React application
- Refactoring a large monolithic store into maintainable slices following domain boundaries
- Implementing persistent state that survives page reloads with automatic schema migrations
- Optimizing component re-renders by choosing between multiple selectors and useShallow
- Debugging state changes in development with devtools integration and action names
- React developers building client-side state management without Redux
- Teams migrating from Redux or Context API to Zustand
- Developers working with TypeScript who need type-safe global state
- Anyone implementing persistent state or complex middleware stacks
zustand-patterns FAQ
Use a single store with slices pattern for easier cross-slice access and state composition. Multiple stores are harder to coordinate.
Use Immer when your state has 3+ levels of nesting. For shallow state, the spread operator is simpler and sufficient.
Always compose as: immer → subscribeWithSelector → devtools → persist. This order ensures immutability, selector support, devtools tracing, and persistence.
Use selectors to access only the state you need. For 2-5 related values, wrap with useShallow. Never destructure the entire store.
No. Use TanStack Query for server state (fetching, caching, synchronization). Zustand is for client-only state like UI flags and form inputs.
Full instructions (SKILL.md)
Source of truth, from yonatangross/orchestkit.
name: zustand-patterns license: MIT compatibility: "Claude Code 2.1.277+." description: "Reference for Zustand 5.x state management including slices, middleware, Immer, useShallow, persistence, selectors, and devtools integration. Documents 7 core patterns with TypeScript examples and anti-patterns. Use when building React state management with Zustand instead of Redux." context: inherit allowed-tools: [Read, Write, Grep, Glob] user-invocable: false disable-model-invocation: true effort: low targets:
- library: zustand version: ">=5.0.0" model: haiku metadata: category: document-asset-creation version: "1.0.0" author: "OrchestKit" complexity: "low" tags: "zustand, state-management, react, immer, middleware, persistence, slices" path_patterns: ["/store/", "/stores/", ".store.", ".zustand."]
Zustand Patterns
Modern state management with Zustand 5.x - lightweight, TypeScript-first, no boilerplate.
Overview
- Global state without Redux complexity
- Shared state across components without prop drilling
- Persisted state with localStorage/sessionStorage
- Computed/derived state with selectors
- State that needs middleware (logging, devtools, persistence)
Upstream coverage (do not restate)
Zustand's own docs are the source for the mechanics. This skill carries only the OrchestKit delta
on top of them, in references/ork-delta.md, the rules/ files, and the checklist.
Quick Reference
// ✅ 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 () => ... }));
Key Decisions
| 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' |
Anti-Patterns & Integration
Forbidden patterns (store destructuring, derived state, server state, direct mutation) and React Query integration guidance.
Load Read("references/anti-patterns-and-integration.md") for anti-pattern examples and TanStack Query separation patterns.
Related Skills
react-server-components-framework- RSC hydration considerations with Zustand- Server state: https://tanstack.com/query/latest/docs/framework/react/guides/does-this-replace-client-state
- Form state: https://react-hook-form.com/docs/useform
Capability Details
store-creation
Keywords: zustand, create, store, typescript, state Solves: Setting up type-safe Zustand stores with proper TypeScript inference
slices-pattern
Keywords: slices, modular, split, combine, StateCreator Solves: Organizing large stores into maintainable, domain-specific slices
middleware-stack
Keywords: immer, persist, devtools, middleware, compose Solves: Combining middleware in correct order for immutability, persistence, and debugging
selector-optimization
Keywords: selector, useShallow, re-render, performance, memoization Solves: Preventing unnecessary re-renders with proper selector patterns
persistence-migration
Keywords: persist, localStorage, sessionStorage, migrate, version Solves: Persisting state with schema migrations between versions
References
Load on demand with Read("references/<file>"):
| File | Content |
|---|---|
ork-delta.md | OrchestKit-specific rules: the corrected zustand/shallow label, the v5 floor, secret handling, graded slice typing |
anti-patterns-and-integration.md | Forbidden patterns and React Query integration |
Other resources:
- Load:
Read("scripts/store-template.ts")- Production-ready store template - Load:
Read("checklists/zustand-checklist.md")- Implementation checklist
Related skills
More from yonatangross/orchestkit and the wider catalog.

agent-orchestration
Patterns for autonomous agent loops, multi-agent coordination, and framework selection.

architecture-decision-record
Nygard-format ADR templates for documenting architectural decisions with context, consequences, and alternatives.

devops-deployment
CI/CD pipelines, containerization, Kubernetes, and infrastructure-as-code patterns for DevOps workflows.

domain-driven-design
DDD tactical patterns for rich domain modeling with entities, value objects, and aggregates.

arxiv-search
Search arXiv preprints with natural language queries powered by semantic search.

pubmed-search
Search PubMed biomedical literature with natural language queries powered by Valyu semantic search. Full-text access, integrate into your AI projects.