zustand
Original:🇺🇸 English
Translated
Zustand state management best practices for React applications. Use when writing, reviewing, or refactoring Zustand stores to ensure optimal performance and maintainability. Triggers on tasks involving state management, stores, selectors, re-renders, and Zustand patterns.
1installs
Sourcepproenca/dot-skills
Added on
NPX Install
npx skill4agent add pproenca/dot-skills zustandTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Community Zustand Best Practices
Comprehensive performance and architecture guide for Zustand state management in React applications. Contains 43 rules across 8 categories, prioritized by impact from critical (store architecture, selector optimization) to incremental (advanced patterns).
When to Apply
Reference these guidelines when:
- Creating new Zustand stores
- Optimizing re-render performance with selectors
- Implementing persistence or middleware
- Integrating Zustand with SSR/Next.js
- Reviewing code for state management patterns
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Store Architecture | CRITICAL | |
| 2 | Selector Optimization | CRITICAL | |
| 3 | Re-render Prevention | HIGH | |
| 4 | State Updates | MEDIUM-HIGH | |
| 5 | Middleware Configuration | MEDIUM | |
| 6 | SSR and Hydration | MEDIUM | |
| 7 | TypeScript Patterns | LOW-MEDIUM | |
| 8 | Advanced Patterns | LOW | |
Quick Reference
1. Store Architecture (CRITICAL)
- - Use multiple small stores instead of one monolithic store
store-multiple-stores - - Separate actions from state in dedicated namespace
store-separate-actions - - Name actions as events not setters
store-event-naming - - Colocate actions with the state they modify
store-colocate-logic - - Derive computed values instead of storing them
store-avoid-derived-state - - Organize stores by feature domain
store-domain-boundaries
2. Selector Optimization (CRITICAL)
- - Always use selectors never subscribe to entire store
select-always-use - - Use atomic selectors for single values
select-atomic-picks - - Ensure selectors return stable references
select-stable-returns - - Export custom hooks not raw store
select-custom-hooks - - Use auto-generated selectors for large stores
select-auto-generate - - Memoize expensive computed selectors
select-memoize-computed - - Define selectors outside components
select-avoid-inline
3. Re-render Prevention (HIGH)
- - Use useShallow for multi-property selections
render-use-shallow - - Provide custom equality functions when needed
render-equality-fn - - Memo children affected by parent store updates
render-memo-children - - Use subscribe for non-React consumers
render-subscribe-external - - Avoid returning new objects from selectors
render-avoid-object-returns - - Split components to minimize subscription scope
render-split-components
4. State Updates (MEDIUM-HIGH)
- - Use functional form when updating based on previous state
update-functional-set - - Never mutate state directly
update-immutable - - Understand set() shallow merge behavior
update-shallow-merge - - Handle async actions with loading and error states
update-async-actions - - Batch related updates in single set call
update-batch-updates
5. Middleware Configuration (MEDIUM)
- - Name actions for DevTools debugging
mw-devtools-actions - - Use partialize for selective persistence
mw-persist-partialize - - Version and migrate persisted state
mw-persist-migration - - Use immer for deeply nested state updates
mw-immer-nested - - Apply middlewares in correct order
mw-combine-order - - Apply middleware at combined store level
mw-slice-middleware
6. SSR and Hydration (MEDIUM)
- - Use skipHydration in SSR contexts
ssr-skip-hydration - - Manually rehydrate on client mount
ssr-manual-rehydrate - - Use custom hook to prevent hydration mismatch
ssr-hydration-hook - - Guard browser APIs with typeof window check
ssr-check-window
7. TypeScript Patterns (LOW-MEDIUM)
- - Use StateCreator for slice typing
ts-state-creator - - Preserve type inference with middleware
ts-middleware-inference - - Separate state and actions interfaces
ts-separate-types - - Type selectors for reusability
ts-generic-selectors - - Type combined stores correctly
ts-bound-store
8. Advanced Patterns (LOW)
- - Combine Zustand with React Context for dependency injection
adv-context-stores - - Use subscribe for transient updates
adv-transient-updates - - Implement computed state with getters
adv-computed-getters - - Integrate with React Query and SWR
adv-third-party-integration
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
Reference Files
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |