Skip to content

secondsky/claude-skills / plugins/zustand-state-management/skills/zustand-state-management

zustand-state-management

Verified

Zustand 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.

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>
 *   )
 * }
 */
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 implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
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 fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
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 alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall