用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/mikailustuner/OmniRule --skill state-management命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
Bun runtime: HTTP server, file I/O, SQLite, test runner, package manager, bundler — all-in-one JS toolchain.
Clerk: Drop-in auth UI, Organizations, User management, JWT templates, webhooks, Next.js middleware integration.
Gelişmiş masaüstü, tarayıcı ve işletim sistemi kontrol yeteneği. Görsel (koordinat tabanlı) fare/klavye otomasyonu, DOM manipülasyonu, pencere yönetimi, gelişmiş dosya, ağ ve süreç yönetimini kapsar.
基于 SOC 职业分类
正在显示 SKILL.md
| name | state-management |
| description | State patterns: When to use what, Zustand vs Context vs Query, Performance strategies, Persistence. |
| triggers | {"extensions":[".tsx",".ts"],"keywords":["state","Zustand","Redux","Jotai","Recoil","context","store","atom","selector"]} |
| auto_load_when | Choosing or implementing state management |
| agent | frontend-ops |
| tools | ["Read","Write","Bash"] |
Focus: Decision trees, when-to-use-what, performance
What type of state?
├── Transient UI state (modal open, tab active)
│ └── useState for component-local
│ └── Zustand for cross-component
│
├── Persisted UI state (theme, user preferences)
│ └── Zustand with persist middleware
│ └── Sync to localStorage/IndexedDB
│
├── Server state (API data, fetched content)
│ └── TanStack Query / SWR (NOT Zustand!)
│ └── Caching, refetching, invalidation built-in
│
└── Complex form state
└── useReducer or Zustand
└── Validation in separate layer
Why NOT Zustand for server state:
Use Zustand for:
├── Global UI state (auth user, theme, sidebar)
├── Shared component state (cart, notifications)
├── Complex client state (multi-step wizard)
└── State with actions (not just data)
Don't use Zustand for:
├── Server data → use TanStack Query
├── Simple local state → use useState
├── High-frequency updates → consider atoms
└── Derived calculations → use selectors
How to organize store:
├── Single store vs multiple stores
│ └── Single: small-medium apps, simple state
│ └── Multiple: large apps, distinct domains
│
├── Normalized vs nested
│ └── Normalized: flat, ID-based references
│ └── Nested: component-friendly, harder to update
│
└── Actions organization
└── By feature (auth: login, logout)
└── By type (user: get, update, delete)
When to optimize (measure first!):
├── Component re-renders on state change
│ └── Use selectors: useStore(s => s.specificField)
│ └── Don't use: useStore() - subscribes to entire state
│
├── Expensive computed values
│ └── Use memoized selectors
│ └── Formula: useStore(s => compute(s.field))
│
└── Frequent updates
└── Batch updates when possible
└── Consider atomic updates (Jotai)
What to persist:
├── User preferences (theme, language)
├── Auth state (token, refresh token)
└── Cart/wishlist (if multi-session)
What NOT to persist:
├── Server data (use server state management)
├── Large datasets
└── Security-critical data (tokens in memory only)
Implementation:
├── Zustand persist middleware
├── Choose storage: localStorage (simple), IndexedDB (large)
└── Handle hydration mismatch
When to use Context:
├── Low-frequency updates (theme, locale)
├── Simple values (no complex logic)
└── App-wide constants
When to use Zustand:
├── Medium-high frequency updates
├── Complex state with actions
├── Need selectors (performance)
└── Need middleware (persist, immer)
Note: Zustand with selectors = better performance than Context in most cases.
What to test:
├── Actions produce correct state changes
├── Selectors return correct values
└── Selectors only re-render when dependencies change
What NOT to test:
├── Implementation details (how state is stored)
├── Internal selector logic (test behavior instead)
└── UI rendering (that's component test)
❌ Global state for everything (even local UI state)
✅ Keep state as local as possible; hoist only when needed
❌ Storing server data in Redux/Zustand (duplicates cache)
✅ Use TanStack Query / SWR for server state; client store for UI state
❌ Deeply nested state objects
✅ Normalize state: entities by ID map + IDs array
❌ Mutating state directly (bypassing immutability)
✅ Immer or spread for immutable updates — never direct mutation
❌ Synchronizing two separate state slices manually
✅ Derive computed values with selectors — don't duplicate state
| State type | Solution | Why |
|---|---|---|
| Server data | TanStack Query / SWR | Cache + deduplicate |
| Global UI | Zustand | Minimal boilerplate |
| Complex global | Redux Toolkit | Time-travel debug |
| Component local | useState | No over-engineering |
| Form | react-hook-form | Performance |
| URL state | searchParams | Shareable, bookmarkable |