PluginBench
Skill
Pass
Audit score 90

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
Prerequisites
  • Zustand >=5.0.0 installed in your project
  • React 18+ (v5 floor)
  • Basic familiarity with React hooks and TypeScript
Claude Code
Cursor
Windsurf
Cline

How to use zustand-patterns

  1. 1.Review the Quick Reference section to understand the double-call pattern and selector basics
  2. 2.Load the store-template.ts script to see a production-ready example
  3. 3.Decide on store structure: single store with slices vs. multiple stores (slices recommended)
  4. 4.Choose middleware stack based on needs: immer for nested updates, persist for localStorage, devtools for debugging
  5. 5.Implement selectors for all state access; use useShallow for 2-5 related values
  6. 6.Load anti-patterns-and-integration.md to identify what NOT to do and how to integrate with TanStack Query
  7. 7.Use the zustand-checklist.md to verify your implementation before shipping

Use cases

Good for
  • 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
Who it's for
  • 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

Should I use a single store or multiple stores?

Use a single store with slices pattern for easier cross-slice access and state composition. Multiple stores are harder to coordinate.

When should I use Immer middleware?

Use Immer when your state has 3+ levels of nesting. For shallow state, the spread operator is simpler and sufficient.

What's the correct middleware order?

Always compose as: immer → subscribeWithSelector → devtools → persist. This order ensures immutability, selector support, devtools tracing, and persistence.

How do I prevent unnecessary re-renders?

Use selectors to access only the state you need. For 2-5 related values, wrap with useShallow. Never destructure the entire store.

Should I store server data in Zustand?

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.

TopicFirst-party source
Basic store, create<State>()(), actions, async actionshttps://github.com/pmndrs/zustand/blob/main/docs/reference/apis/create.md
Slices pattern (splitting and combining stores)https://github.com/pmndrs/zustand/blob/main/docs/learn/guides/slices-pattern.md
Typing slices and middleware mutator tupleshttps://github.com/pmndrs/zustand/blob/main/docs/learn/guides/advanced-typescript.md
Immer middleware (draft mutations)https://github.com/pmndrs/zustand/blob/main/docs/reference/middlewares/immer.md
persist: partialize, version, migrate, onRehydrateStorage, storage adaptershttps://github.com/pmndrs/zustand/blob/main/docs/reference/middlewares/persist.md
devtools: action names, enabled, serialize, tracehttps://github.com/pmndrs/zustand/blob/main/docs/reference/middlewares/devtools.md
subscribeWithSelector and non-React subscriptionshttps://github.com/pmndrs/zustand/blob/main/docs/reference/middlewares/subscribe-with-selector.md
Selectors and useShallow re-render controlhttps://github.com/pmndrs/zustand/blob/main/docs/learn/guides/prevent-rerenders-with-use-shallow.md
v4 to v5 migration (createWithEqualityFn, React 18 floor)https://github.com/pmndrs/zustand/blob/main/docs/reference/migrations/migrating-to-v5.md
SSR and hydrationhttps://github.com/pmndrs/zustand/blob/main/docs/learn/guides/ssr-and-hydration.md
Store testing and resethttps://github.com/pmndrs/zustand/blob/main/docs/learn/guides/testing.md
Server-state ownership (use TanStack Query, not Zustand)https://tanstack.com/query/latest/docs/framework/react/guides/does-this-replace-client-state

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

DecisionOption AOption BRecommendation
State structureSingle storeMultiple storesSlices in single store - easier cross-slice access
Nested updatesSpread operatorImmer middlewareImmer for deeply nested state (3+ levels)
PersistenceManual localStoragepersist middlewarepersist middleware with partialize
Multiple valuesMultiple selectorsuseShallowuseShallow for 2-5 related values
Server stateZustandTanStack QueryTanStack Query - Zustand for client-only state
DevToolsAlways onConditionalConditional - 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

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>"):

FileContent
ork-delta.mdOrchestKit-specific rules: the corrected zustand/shallow label, the v5 floor, secret handling, graded slice typing
anti-patterns-and-integration.mdForbidden 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