mcpField
R
react-state

react-state

github.com/fusengineDevelopment3443 installsUpdated 2w ago
Repository

About react-state

react-state is a skill for implementing Zustand v5 state management in React applications. It focuses on typed store creation, selector performance, middleware composition, and domain-based organization, while keeping server state in TanStack Query.

Key features

  1. 01Zustand v5 TypeScript store patterns
  2. 02Selector and useShallow guidance
  3. 03devtools, persist, and immer composition
  4. 04Domain-separated store organization
  5. 05v4 migration and anti-pattern guidance

Use cases

  1. 01Add shared state to a React application
  2. 02Organize auth, cart, and UI stores
  3. 03Persist selected state without auth tokens
  4. 04Migrate store code from Zustand v4

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/fusengine/agents --skill react-state

After installing, type/react-statein Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
auto-selectors.md
installation.md
middleware.md
migration-v5.md
reset-state.md
slices.md
store-patterns.md
subscribe-api.md
testing.md
typescript.md

Details

From the project README
Implements Zustand v5 global state management for React applications: the v5 currying syntax (`create()((set) => ({...}))`), the selector pattern (`useStore((s) => s.field)`), `useShallow` for array/object selectors to prevent unnecessary re-renders, and middleware composition (devtools → persist → immer, order-sensitive for TypeScript).

Covers 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:

  1. fuse-ai-pilot:explore-codebase - Analyze existing stores and state patterns
  2. fuse-ai-pilot:research-expert - Verify latest Zustand v5 docs via Context7/Exa
  3. 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
FeatureBenefit
Minimal APISimple create() function, no boilerplate
React 18 nativeuseSyncExternalStore, no shims needed
TypeScript firstFull inference with currying pattern
Middleware stackdevtools, persist, immer composable
Bundle size~2KB gzipped, smallest state library
No providersDirect store access, no Context wrapper

Critical Rules

  1. useShallow for arrays/objects - Prevent unnecessary re-renders
  2. Currying syntax v5 - create<State>()((set) => ({...}))
  3. SOLID paths - Stores in modules/[feature]/src/stores/
  4. Separate stores - One store per domain (auth, cart, ui, theme)
  5. 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 state
  • modules/cart/src/stores/ - Cart state
  • modules/[feature]/src/interfaces/ - Store types
File Organization
FilePurposeMax Lines
store.tsStore creation with create()50
store.interface.tsTypeScript interfaces30
use-store.tsCustom hook with selector20

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

NeedReference
Initial setupinstallation.md
Store patternsstore-patterns.md
Middlewaremiddleware.md
TypeScripttypescript.md
Slices patternslices.md
Auto selectorsauto-selectors.md
Reset statereset-state.md
Subscribe APIsubscribe-api.md
Testingtesting.md
Migration v4→v5migration-v5.md

Best Practices

  1. Selector pattern - Always use useStore((s) => s.field) for performance
  2. useShallow - Wrap array/object selectors to prevent re-renders
  3. Separate stores - One store per domain (auth, cart, ui, theme)
  4. Server data elsewhere - Use TanStack Query for server state
  5. DevTools in dev only - Wrap devtools in process.env check
  6. Partialize persist - Only persist necessary fields, never tokens

Forbidden Patterns

PatternReasonAlternative
Persisting auth tokensSecurity vulnerabilityhttpOnly cookies
Without useShallow on objectsExcessive re-rendersuseShallow(selector)
v4 syntaxTypeScript inference brokenv5 currying create<T>()()
Giant monolithic storeHard to maintainSlices or separate stores

Comments

Markdown supported