| name | zustand |
| description | Expert guide for Zustand state management patterns, store organization, and best practices. Use when implementing client state management with Zustand, creating stores, or managing shared UI state across components. |
| allowed-tools | Read, Grep, Glob |
Zustand State Management Guide
This skill provides guidelines, patterns, and best practices for working with Zustand in this project.
Quick Start
For detailed store patterns, middleware usage, and comprehensive examples, please refer to references/patterns.md.
Core Philosophy
- Shared Client State Only: Use Zustand for shared client state, not server state (use TanStack Query for that).
- Domain-Specific Stores: Keep stores focused on specific domains.
- Type Safety: Leverage TypeScript for fully typed stores.
- Simplicity: Prefer simplicity over complex abstractions.
Store Organization
Essential Patterns
- Create separate stores for different domains
- Use slices pattern for large stores
- Keep stores close to features that use them
- Export typed selector hooks for better DX and performance
Recommended Middleware Stack
Use the following middleware combination for production stores:
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import { immer } from "zustand/middleware/immer";
export const useExampleStore = create<ExampleState>()(
devtools(
persist(
immer((set, get) => ({
})),
{ name: "example-storage" }
),
{ name: "example-store" }
)
);
Store Structure Template
interface StoreState {
data: DataType | null;
isLoading: boolean;
actions: {
fetchData: () => Promise<void>;
updateData: (updates: Partial<DataType>) => void;
reset: () => void;
};
}
Selector Hooks Pattern
Always create selector hooks for performance optimization:
const { user, isLoading } = useAuthStore();
export const useUser = () => useAuthStore(state => state.user);
export const useIsLoading = () => useAuthStore(state => state.isLoading);
export const useAuthActions = () => useAuthStore(state => state.actions);
Best Practices
- Keep stores focused on specific domains
- Use TypeScript for full type safety
- Leverage middleware for common patterns (devtools, persist, immer)
- Create selector hooks for performance
- Use immer for complex nested state updates
- Persist only necessary state - use
partialize option
- Test stores thoroughly
- Handle async operations properly with loading/error states
- Implement optimistic updates when appropriate
- Document store structure and actions
Common Tasks
Creating a New Store
- Define the state interface with typed actions
- Create the store with appropriate middleware
- Export selector hooks for each state slice
- Add to the feature's barrel export
Persisting State
Use the persist middleware with partialize to persist only necessary data:
persist(
set => ({
}),
{
name: "store-key",
partialize: state => ({
user: state.user,
preferences: state.preferences,
}),
}
);
Using Immer for Updates
Immer allows mutable-style updates that produce immutable state:
immer(set => ({
updateNested: (id, value) => {
set(state => {
const item = state.items.find(i => i.id === id);
if (item) {
item.value = value;
}
});
},
}));
Validation Checklist
Before finishing a task involving Zustand:
For detailed rules, examples, and anti-patterns, please consult references/patterns.md.