mcpField
R
react-state

react-state

github.com/fusengineDevelopment3443 installsUpdated 1d ago
Repository

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

  1. 01Zustand v5 curried store creation
  2. 02Selector-based state access
  3. 03useShallow for array and object selectors
  4. 04Composable devtools, persist, and immer middleware
  5. 05Domain-based store organization
  6. 06TypeScript-oriented file structure

Use cases

  1. 01Set up shared state across React components
  2. 02Organize auth, cart, UI, and theme stores
  3. 03Reduce rerenders from array or object selectors
  4. 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-state

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

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
FilePurpose
store.tsStore creation with create() (max 40 lines, owner rule)
store.interface.tsTypeScript interfaces
use-store.tsCustom 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

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