AgentSkillsCN

State Management

状态管理

SKILL.md

State Management Skill

Zustand-based state management standards and best practices.

When to Apply

  • Creating new state stores
  • Sharing state between components
  • State architecture decisions
  • Performance optimization for state

State Category Guidelines

CategorySolutionUse When
Local UIuseStateSingle component, simple state
Shared UIZustand storeMultiple components need same data
Server dataReact Query / SWRAPI data with caching needs
Form stateuseState / form libraryForm inputs, validation
URL stateuseSearchParamsFilter/sort params, shareable URLs

Zustand Store Standards

Store Structure

ElementRequirement
InterfaceDefine typed state + actions interface
StateGroup related state together
ActionsDefine all mutations as functions
Naminguse[Domain]Store convention

Store Design Principles

PrincipleDescription
Single responsibilityOne store per domain (projects, UI, settings)
Flat structureAvoid deeply nested state
Immutable updatesAlways return new objects
Colocated actionsKeep actions inside store definition

Selector Best Practices

Selection Rules

RuleReason
Select minimal dataReduces re-renders
Use shallow for objectsPrevents unnecessary updates
Memoize derived dataUse useMemo for computed values
Avoid selecting entire storeCauses re-render on any change

Selector Patterns

PatternUse Case
Single value(state) => state.count
Multiple values(state) => ({ a: state.a, b: state.b }), shallow
Derived valueuseMemo outside store

Middleware Usage

MiddlewarePurposeWhen to Use
persistLocalStorage persistenceUser preferences, settings
devtoolsRedux DevTools integrationDevelopment debugging
immerImmutable updatesComplex nested state
subscribeWithSelectorGranular subscriptionsPerformance optimization

Store Organization

File Structure

code
src/stores/
├── index.ts           # Re-exports all stores
├── projectStore.ts    # Domain-specific store
├── uiStore.ts         # UI state (modals, panels)
└── settingsStore.ts   # User preferences

Store Separation Guidelines

Store TypeContains
Domain storeBusiness entities, selections
UI storeModal states, panel visibility, loading
Settings storeUser preferences, persisted config

Performance Guidelines

DO

  • Select only needed state slices
  • Use shallow equality for object selections
  • Define actions inside store (stable references)
  • Split large stores by domain
  • Use subscribeWithSelector for side effects

DON'T

  • Select entire store object
  • Create new objects in selectors without shallow
  • Put derived/computed state in store
  • Call actions during render phase
  • Create deeply nested store structure

Anti-Patterns

Anti-PatternProblemSolution
Giant storeHard to maintain, performance issuesSplit by domain
Computed in storeStale data, extra complexityUse useMemo
Prop drillingBypasses store benefitsUse store directly
Store in useStateLoses reactivityUse store hook
No TypeScriptRuntime errorsDefine interfaces

Decision Matrix

ScenarioRecommended Solution
Form input stateuseState
Modal open/closeuseState or UI store
Selected item (shared)Domain store
User preferencesPersisted store
API response dataStore + service function
Computed/derived datauseMemo from store values
Global loading stateUI store