secondsky/claude-skills / plugins/zustand-state-management/skills/zustand-state-management
zustand-state-management
VerifiedZustand state management for React with TypeScript. Use for global state, Redux/Context API migration, localStorage persistence, slices pattern, devtools, Next.js SSR, or encountering hydration errors, TypeScript inference issues, persist middleware problems, infinite render loops.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 9.7 KB |
| assets/example-template.txt | markdown | 416 B |
| references/advanced-topics.md | markdown | 7.9 KB |
| references/common-patterns.md | markdown | 6.4 KB |
| references/example-reference.md | markdown | 760 B |
| references/known-issues.md | markdown | 5.4 KB |
| references/middleware-guide.md | markdown | 5.2 KB |
| references/migration-guide.md | markdown | 6.2 KB |
| references/nextjs-hydration.md | markdown | 5.0 KB |
| references/typescript-patterns.md | markdown | 4.9 KB |
| scripts/check-versions.shexecutable | script | 3.6 KB |
| scripts/example-script.shexecutable | script | 302 B |
| templates/async-actions-store.ts | script | 7.6 KB |
| templates/basic-store.ts | script | 908 B |
| templates/computed-store.ts | script | 5.6 KB |
| templates/devtools-store.ts | script | 3.4 KB |
| templates/nextjs-store.ts | script | 2.6 KB |
| templates/persist-store.ts | script | 2.8 KB |
| templates/slices-pattern.ts | script | 5.0 KB |
| templates/typescript-store.ts | script | 1.8 KB |
templates/nextjs-store.ts
View on GitHub/**
* Next.js SSR-Safe Zustand Store with Hydration Handling
*
* Use when:
* - Using persist middleware in Next.js
* - Getting hydration mismatch errors
* - Need SSR compatibility
*
* This template solves:
* - "Text content does not match server-rendered HTML"
* - "Hydration failed" errors
* - Flashing content during hydration
*
* Learn more: See SKILL.md Issue #1 for detailed explanation
*/
'use client' // Required for Next.js App Router
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'
interface User {
id: string
name: string
}
interface NextJsStore {
// State
_hasHydrated: boolean // CRITICAL: Track hydration status
count: number
user: User | null
// Actions
setHasHydrated: (hydrated: boolean) => void
increment: () => void
setUser: (user: User | null) => void
reset: () => void
}
export const useNextJsStore = create<NextJsStore>()(
persist(
(set) => ({
// Initial state
_hasHydrated: false, // Start false
count: 0,
user: null,
// Hydration action
setHasHydrated: (hydrated) => set({ _hasHydrated: hydrated }),
// Regular actions
increment: () => set((state) => ({ count: state.count + 1 })),
setUser: (user) => set({ user }),
reset: () => set({ count: 0, user: null }),
}),
{
name: 'nextjs-storage',
// CRITICAL: Call setHasHydrated when rehydration completes
onRehydrateStorage: () => (state) => {
state?.setHasHydrated(true)
},
},
),
)
/**
* Usage in Next.js component:
*
* 'use client'
*
* import { useNextJsStore } from './store'
*
* function Counter() {
* const hasHydrated = useNextJsStore((state) => state._hasHydrated)
* const count = useNextJsStore((state) => state.count)
* const increment = useNextJsStore((state) => state.increment)
*
* // Show loading state during hydration
* if (!hasHydrated) {
* return <div>Loading...</div>
* }
*
* // Now safe to render with persisted state
* return (
* <div>
* <p>Count: {count}</p>
* <button onClick={increment}>Increment</button>
* </div>
* )
* }
*
* Alternative: Skip hydration check if flashing is acceptable
*
* function CounterNoLoadingState() {
* const count = useNextJsStore((state) => state.count)
* const increment = useNextJsStore((state) => state.increment)
*
* // Will show default value (0) until hydration, then correct value
* return (
* <div>
* <p>Count: {count}</p>
* <button onClick={increment}>Increment</button>
* </div>
* )
* }
*/
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |