AgentSkillsCN

zustand-store

适用于 Flux 项目的 Zustand 状态管理模式:切片模式、严格选择器,以及应用级状态存储定义。当您需要创建新的 Zustand 状态存储、添加状态切片、编写选择器、将状态存储与 React 组件集成,或重构客户端状态管理时,可选用此模式。本技能将自动触发与 Zustand 状态管理、状态存储创建、选择器使用,以及客户端 UI 状态相关的任务。

SKILL.md
--- frontmatter
name: zustand-store
description: Zustand state management patterns for the Flux project. Slice pattern, strict selectors, and app-local store definitions. Use when creating new zustand stores, adding slices, writing selectors, integrating stores with React components, or refactoring client-side state management. Triggers on tasks involving zustand, store creation, state management, selectors, or client UI state.

Zustand Store

Zustand state management with Slice architecture for the Flux monorepo.

Architecture

  • Factory: apps/web/src/lib/create-store.ts - the createStore wrapper, local to web app
  • Store definitions: apps/web/src/stores/ - domain stores live in the consuming app
  • Store creation: createWithEqualityFn + shallow default + optional devtools + subscribeWithSelector
  • Organization: Multi-store by domain, each store uses Slice pattern
  • Selectors: Strict mode - all state access through selector objects
  • Types: Full TypeScript - Store = State & SliceAction1 & SliceAction2 & ...

Quick Reference

PatternReference
Store creation & Slice patternstore-patterns
Selector patternsselector-patterns
React integrationreact-integration

Directory Structure

App-local stores (factory + domain stores):

code
apps/web/src/stores/
  └── <domain>/
      ├── store.ts             # store creation + slice composition
      ├── initial-state.ts     # state types + initial values
      ├── index.ts             # public exports
      └── slices/
          └── <slice>/
              ├── action.ts
              ├── initial-state.ts
              └── selectors.ts

Rules

  1. Never access store state directly in components - always use selectors
  2. Every selector function must be exported via a xxxSelectors object
  3. Use shallow as default equality fn; use isEqual for deep objects only when needed
  4. Prefix internal-only actions with internal_
  5. Keep state flat - avoid deeply nested objects
  6. Async actions call services then set() - no middleware for persistence
  7. Use subscribeWithSelector on stores that need external subscriptions