About react-state
A skill for implementing Zustand v5 global state management in React applications. It targets developers organizing shared client-side state and covers typed store creation, selector usage, and middleware composition.
Key features
- 01Zustand v5 curried store creation
- 02Selector-based state access
- 03useShallow for array and object selectors
- 04Composable devtools, persist, and immer middleware
- 05Domain-based store organization
- 06TypeScript-oriented file structure
Use cases
- 01Set up shared state across React components
- 02Organize auth, cart, UI, and theme stores
- 03Reduce rerenders from array or object selectors
- 04Compose middleware with TypeScript
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ fusengine/ claude-plugins --skill react-stateAfter installing, type/react-statein Claude Code to confirm it loaded.
Details
From the project READMECovers separating stores by domain (auth, cart, ui, theme) rather than one giant store, SOLID file organization (store.ts, store.interface.ts, use-store.ts), and forbidden patterns (persisting auth tokens, v4 syntax, skipping useShallow). This is the plain-React variant of Zustand (no App Router request-isolation concerns) — for Next.js App Router see nextjs-zustand, and for server state use TanStack Query instead of this skill.
Zustand for React
Minimal, scalable state management with React 18+ useSyncExternalStore.
Agent Workflow (MANDATORY)
Before ANY implementation, spawn 3 agents in parallel, one Agent call each with a name:
- fuse-ai-pilot:explore-codebase - Analyze existing stores and state patterns
- fuse-ai-pilot:research-expert - Verify latest Zustand v5 docs via Context7/Exa
- mcp__context7__query-docs - Check middleware and TypeScript patterns
After implementation, run fuse-ai-pilot:sniper for validation.
Overview
When to Use
- Managing global state in React applications
- Need state shared across components
- Persisting state to localStorage/sessionStorage
- Building UI state (modals, sidebars, theme, cart)
- Replacing React Context for complex state
Why Zustand v5
| Feature | Benefit |
|---|---|
| Minimal API | Simple create() function, no boilerplate |
| React 18 native | useSyncExternalStore, no shims needed |
| TypeScript first | Full inference with currying pattern |
| Middleware stack | devtools, persist, immer composable |
| Bundle size | ~2KB gzipped, smallest state library |
| No providers | Direct store access, no Context wrapper |
Critical Rules
- useShallow for arrays/objects - Prevent unnecessary re-renders
- Currying syntax v5 -
create<State>()((set) => ({...})) - SOLID paths - Stores in
modules/[feature]/src/stores/ - Separate stores - One store per domain (auth, cart, ui, theme)
- Server state elsewhere - Use TanStack Query for server state
SOLID Architecture
Module Structure
Stores organized by feature module:
modules/cores/stores/- Shared stores (theme, ui)modules/auth/src/stores/- Auth statemodules/cart/src/stores/- Cart statemodules/[feature]/src/interfaces/- Store types
File Organization
| File | Purpose |
|---|---|
store.ts | Store creation with create() (max 40 lines, owner rule) |
store.interface.ts | TypeScript interfaces |
use-store.ts | Custom hook with selector |
Key Concepts
Store Creation (v5 Syntax)
Double parentheses required for TypeScript inference. Currying pattern ensures full type safety.
Middleware Composition
Stack middlewares: devtools -> persist -> immer. Order matters for TypeScript types.
Selector Pattern
Always use useStore((s) => s.field) for performance. Use useShallow for array/object selectors.
Reference Guide
| Need | Reference |
|---|---|
| Initial setup | installation.md |
| Store patterns | store-patterns.md |
| Middleware | middleware.md |
| TypeScript | typescript.md |
| Slices pattern | slices.md |
| Auto selectors | auto-selectors.md |
| Reset state | reset-state.md |
| Subscribe API | subscribe-api.md |
| Testing | testing.md |
| Migration v4→v5 | migration-v5.md |
Best Practices
- Selector pattern - Always use
useStore((s) => s.field)for performance - useShallow - Wrap array/object selectors to prevent re-renders
- Separate stores - One store per domain (auth, cart, ui, theme)
- Server data elsewhere - Use TanStack Query for server state
- DevTools in dev only - Wrap devtools in process.env check
- Partialize persist - Only persist necessary fields, never tokens
Forbidden Patterns
| Pattern | Reason | Alternative |
|---|---|---|
| Persisting auth tokens | Security vulnerability | httpOnly cookies |
| Without useShallow on objects | Excessive re-renders | useShallow(selector) |
| v4 syntax | TypeScript inference broken | v5 currying create<T>()() |
| Giant monolithic store | Hard to maintain | Slices or separate stores |