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/slices-pattern.ts

View on GitHub
/**
 * Zustand Slices Pattern (Modular Store Architecture)
 *
 * Use when:
 * - Store is getting large (100+ lines)
 * - Multiple developers working on store
 * - Need to organize state by domain/feature
 *
 * Benefits:
 * - Each slice is independent and testable
 * - Slices can access each other's state/actions
 * - Easy to add/remove features
 * - Better code organization
 *
 * Learn more: See SKILL.md Issue #5 for TypeScript complexity handling
 */

import { create, StateCreator } from 'zustand'
import { devtools } from 'zustand/middleware'

// ============================================================================
// SLICE 1: User Management
// ============================================================================

interface User {
  id: string
  name: string
  email: string
}

interface UserSlice {
  user: User | null
  isAuthenticated: boolean
  setUser: (user: User) => void
  logout: () => void
}

const createUserSlice: StateCreator<
  UserSlice & CartSlice & NotificationSlice,  // Combined type
  [['zustand/devtools', never]],               // Middleware mutators
  [],                                          // Chained middleware
  UserSlice                                     // This slice's type
> = (set) => ({
  user: null,
  isAuthenticated: false,

  setUser: (user) =>
    set(
      { user, isAuthenticated: true },
      undefined,
      'user/setUser',
    ),

  logout: () =>
    set(
      { user: null, isAuthenticated: false },
      undefined,
      'user/logout',
    ),
})

// ============================================================================
// SLICE 2: Shopping Cart
// ============================================================================

interface CartItem {
  id: string
  name: string
  price: number
  quantity: number
}

interface CartSlice {
  items: CartItem[]
  addItem: (item: CartItem) => void
  removeItem: (id: string) => void
  clearCart: () => void
  // Access other slices
  eatFish: () => void // Example: cross-slice action
}

const createCartSlice: StateCreator<
  UserSlice & CartSlice & NotificationSlice,
  [['zustand/devtools', never]],
  [],
  CartSlice
> = (set, get) => ({
  items: [],

  addItem: (item) =>
    set(
      (state) => ({
        items: [...state.items, item],
      }),
      undefined,
      'cart/addItem',
    ),

  removeItem: (id) =>
    set(
      (state) => ({
        items: state.items.filter((item) => item.id !== id),
      }),
      undefined,
      'cart/removeItem',
    ),

  clearCart: () =>
    set(
      { items: [] },
      undefined,
      'cart/clearCart',
    ),

  // Example: Action that accesses another slice
  eatFish: () =>
    set(
      (state) => ({
        // Access notification slice
        notifications: state.notifications + 1,
      }),
      undefined,
      'cart/eatFish',
    ),
})

// ============================================================================
// SLICE 3: Notifications
// ============================================================================

interface NotificationSlice {
  notifications: number
  messages: string[]
  addNotification: (message: string) => void
  clearNotifications: () => void
}

const createNotificationSlice: StateCreator<
  UserSlice & CartSlice & NotificationSlice,
  [['zustand/devtools', never]],
  [],
  NotificationSlice
> = (set) => ({
  notifications: 0,
  messages: [],

  addNotification: (message) =>
    set(
      (state) => ({
        notifications: state.notifications + 1,
        messages: [...state.messages, message],
      }),
      undefined,
      'notifications/add',
    ),

  clearNotifications: () =>
    set(
      { notifications: 0, messages: [] },
      undefined,
      'notifications/clear',
    ),
})

// ============================================================================
// COMBINE SLICES
// ============================================================================

export const useAppStore = create<UserSlice & CartSlice & NotificationSlice>()(
  devtools(
    (...a) => ({
      ...createUserSlice(...a),
      ...createCartSlice(...a),
      ...createNotificationSlice(...a),
    }),
    { name: 'AppStore' },
  ),
)

/**
 * Usage in components:
 *
 * function UserProfile() {
 *   const user = useAppStore((state) => state.user)
 *   const logout = useAppStore((state) => state.logout)
 *
 *   if (!user) return <div>Not logged in</div>
 *
 *   return (
 *     <div>
 *       <p>Welcome, {user.name}!</p>
 *       <button onClick={logout}>Logout</button>
 *     </div>
 *   )
 * }
 *
 * function Cart() {
 *   const items = useAppStore((state) => state.items)
 *   const clearCart = useAppStore((state) => state.clearCart)
 *
 *   return (
 *     <div>
 *       <h2>Cart ({items.length} items)</h2>
 *       <button onClick={clearCart}>Clear</button>
 *     </div>
 *   )
 * }
 *
 * ORGANIZATION TIP:
 * In larger projects, put each slice in its own file:
 *
 * src/store/
 *   ├── index.ts           (combines slices)
 *   ├── userSlice.ts
 *   ├── cartSlice.ts
 *   └── notificationSlice.ts
 */
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