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/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
*/
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 |