install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/zustand" ~/.claude/skills/comeonoliver-skillshub-zustand && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/zustand/SKILL.mdsource content
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 storestore-multiple-stores
- Separate actions from state in dedicated namespacestore-separate-actions
- Name actions as events not settersstore-event-naming
- Colocate actions with the state they modifystore-colocate-logic
- Derive computed values instead of storing themstore-avoid-derived-state
- Organize stores by feature domainstore-domain-boundaries
2. Selector Optimization (CRITICAL)
- Always use selectors never subscribe to entire storeselect-always-use
- Use atomic selectors for single valuesselect-atomic-picks
- Ensure selectors return stable referencesselect-stable-returns
- Export custom hooks not raw storeselect-custom-hooks
- Use auto-generated selectors for large storesselect-auto-generate
- Memoize expensive computed selectorsselect-memoize-computed
- Define selectors outside componentsselect-avoid-inline
3. Re-render Prevention (HIGH)
- Use useShallow for multi-property selectionsrender-use-shallow
- Provide custom equality functions when neededrender-equality-fn
- Memo children affected by parent store updatesrender-memo-children
- Use subscribe for non-React consumersrender-subscribe-external
- Avoid returning new objects from selectorsrender-avoid-object-returns
- Split components to minimize subscription scoperender-split-components
4. State Updates (MEDIUM-HIGH)
- Use functional form when updating based on previous stateupdate-functional-set
- Never mutate state directlyupdate-immutable
- Understand set() shallow merge behaviorupdate-shallow-merge
- Handle async actions with loading and error statesupdate-async-actions
- Batch related updates in single set callupdate-batch-updates
5. Middleware Configuration (MEDIUM)
- Name actions for DevTools debuggingmw-devtools-actions
- Use partialize for selective persistencemw-persist-partialize
- Version and migrate persisted statemw-persist-migration
- Use immer for deeply nested state updatesmw-immer-nested
- Apply middlewares in correct ordermw-combine-order
- Apply middleware at combined store levelmw-slice-middleware
6. SSR and Hydration (MEDIUM)
- Use skipHydration in SSR contextsssr-skip-hydration
- Manually rehydrate on client mountssr-manual-rehydrate
- Use custom hook to prevent hydration mismatchssr-hydration-hook
- Guard browser APIs with typeof window checkssr-check-window
7. TypeScript Patterns (LOW-MEDIUM)
- Use StateCreator for slice typingts-state-creator
- Preserve type inference with middlewarets-middleware-inference
- Separate state and actions interfacests-separate-types
- Type selectors for reusabilityts-generic-selectors
- Type combined stores correctlyts-bound-store
8. Advanced Patterns (LOW)
- Combine Zustand with React Context for dependency injectionadv-context-stores
- Use subscribe for transient updatesadv-transient-updates
- Implement computed state with gettersadv-computed-getters
- Integrate with React Query and SWRadv-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 |