zustand-state-patterns
/zustand-state-patterns
Podpowiada produkcyjne wzorce Zustand: typowane magazyny ze slice'ami, selektory, asynchroniczne akcje, odpytywanie statusu z czyszczeniem i persist.
- Autor
- ajyadav013 ↗
- Repozytorium
- claude-kit ↗
- Licencja
- MIT
- Ostatnia zmiana repozytorium
- 07.09.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 4
- Skrypty
- 0
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 1
Brak sygnałów. Repozytorium zmienione 07.09.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
Co robi i kiedy
Podpowiada produkcyjne wzorce Zustand: typowane magazyny ze slice'ami, selektory, asynchroniczne akcje, odpytywanie statusu z czyszczeniem i persist.
Oryginalny opis (EN)
Zustand production patterns — typed stores with StateCreator slices, exported selectors, async actions, status polling with cleanup, persist middleware. Use for client state or async workflows with progress.
Jak działa
- When to use
- Core conventions
- Skeleton / example
- Anti-patterns to avoid
- References
Podgląd SKILL.md
---
name: zustand-state-patterns
description: Zustand production patterns — typed stores with StateCreator slices, exported selectors, async actions, status polling with cleanup, persist middleware. Use for client state or async workflows with progress.
---
Standardize Zustand state management structure, async actions, polling patterns, and persistence following production frontend patterns.
## When to use
- Scaffolding a new Zustand store for a domain or feature
- Implementing authentication state with session restoration and token refresh
- Building async workflows with job polling and progress tracking
- Adding optimistic UI updates before API responses resolve
- Migrating from Redux or Context API to a simpler state solution
- Setting up multi-tenant or workspace context stores
- Implementing real-time data polling (e.g., video generation status, job progress)
- Designing computed selectors for performance optimization
- Integrating devtools middleware for debugging state transitions
- Adding persistence middleware for localStorage/sessionStorage
- Preserving state across HMR during development
## Core conventions
1. **Typed store with State + Actions interface**: define a single interface that declares both state fields (primitives, arrays, objects, loading flags) and action methods (async functions, setters). Use `create<MyState>()((set, get) => ({ ... }))` with the full interface as the type parameter.
2. **StateCreator slices for complex stores**: break large stores into logical slices using `StateCreator<MyState>`. Each slice returns a partial state object; combine them via spread `{ ...authSlice(set, get), ...profileSlice(set, get) }`. This pattern is optional but useful for stores with 10+ actions.
3. **Exported selector functions**: export named selectors like `export const selectUser = (state: AuthState) => state.user;` for trivial field access and `export const selectIsAuthenticated = (state: AuthState) => state.isAuthenticated;` for computed values. Use selectors in components via `const user = useAuthStore(selectUser);` to minimize re-renders (only re-render when the selected slice changes). Add JSDoc for non-trivial selectors.
4. **Async actions with try/catch and loading flags**: all async actions set `isLoading: true` before the API call, handle errors in a catch block, and reset `isLoading: false` in finally or catch. Store error messages in `error: string | null` state. Example: `login: async (email, password) => { set({ isLoading: true }); try { ... } catch { set({ error: ... }); } finally { set({ isLoading: false }); } }`.
5. **Optimistic updates**: for actions that update UI immediately before the API responds, add the new item to the state array/object first, then call the API, and rollback or reconcile on error. Example: in chat, add user message to `messages` array optimistically before sending, then append assistant response when polling completes.
6. **Polling pattern with setInterval and cleanup**: store the interval ID in state (`pollingInterval: ReturnType<typeof setInterval> | null`), start polling in a `startPolling()` action via `setInterval(async () => { ... }, INTERVAL_MS)`, and stop polling in `stopPolling()` via `clearInterval()`. Always call `stopPolling()` on success/error/unmount. Example use cases: video generation status, async job progress, real-time data updates.
7. **HMR persistence wrapper**: for stores that should survive hot module replacement during development, wrap the StateCreator with a custom `persistHMR(name, config)` middleware that saves state to `import.meta.hot.data` on dispose and restores it on reload. This is dev-only and does not affect production builds.
8. **persist middleware for localStorage/sessionStorage**: use `persist((set, get) => ({ ... }), { name: 'storage-key', partialize: (state) => ({ field1: state.field1, ... }) })` to persist only selected fields to storage. Use `sessionStorage` for session-scoped data (auth tokens) and `localStorage` for user preferences. The `partialize` function controls which fields are persisted (omit sensitive data, transient UI state, or large objects).
9. **devtools middleware for debugging**: wrap the store with `devtools((set, get) => ({ ... }), { name: 'MyStore' })` to enable Redux DevTools integration. Use for complex stores with many actions or when debugging state transitions is needed.
10. **Immutable nested updates via spread**: always use spread operators to update nested objects/arrays immutably. Example: `set((state) => ({ items: [...state.items, newItem] }))` or `set((state) => ({ user: { ...state.user, name: newName } }))`. Never mutate state directly (`state.items.push(newItem)` is forbidden).
11. **Action naming conventions**: use imperative verbs for actions (`login`, `logout`, `loadReports`, `approveDocument`, `setFilters`) and `select*` prefix for selectors (`selectUser`, `selectCurrentTenant`, `selectIsLoading`). Boolean flags use `is*` or `has*` prefix (`isLoading`, `isAuthenticated`, `hasUnreadMessages`).
12. **Initial state constant**: define `const initialState = { ... }` for the default state shape and reuse it in the store and in reset actions. This ensures consistent state structure and simplifies reset logic.
13. **Session restoration pattern**: for auth stores, implement a `checkAuth()` action that runs on app mount to restore the session from persisted tokens or storage. Handle network errors gracefully (don't log out on transient failures). Use `isInitializing` flag to distinguish "checking session" from "ready".
14. **Tenant/workspace switching**: for multi-tenant apps, store the active tenant/workspace in state and provide a `switchTenant(id)` action that calls the backend to get new scoped tokens, then updates local state. Preserve the selected tenant in persisted state so it survives page reloads.
15. **Pagination state**: for paginated list stores, track `pagination: { page, page_size, total, total_pages } | null` and a `filters` object. When filters change, reset `page` to 1. Example: `setFilters(newFilters) => { set({ filters: { ...filters, ...newFilters, page: 1 } }); loadData(); }`.
## Skeleton / example
```typescript
// stores/authStore.ts
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import type { User, Tenant, UserRole } from '@/types';
import { authService } from '@/lib/auth';
interface AuthState {
// State
user: User | null;
isAuthenticated: boolean;
isInitializing: boolean;
isLoading: boolean;
currentTenant: Tenant | null;
availableTenants: Tenant[];
userRole: UserRole;
// Actions
login: (email: string, password: string) => Promise<void>;
logout: () => Promise<void>;
checkAuth: () => Promise<void>;
switchTenant: (tenantId: string) => Promise<void>;
updateProfile: (data: { name?: string; phone?: string }) => Promise<User>;
}
const initialState = {
user: null,
isAuthenticated: false,
isInitializing: true,
isLoading: false,
currentTenant: null,
availableTenants: [],
userRole: 'viewer' as UserRole,
};
export const useAuthStore = create<AuthState>()(
persist(
(set, get) => ({
...initialState,
login: async (email: string, password: string) => {
set({ isLoading: true });
try {
await authService.login(email, password);
const { user, tenants } = await fetchUserAndTenants();
// Switch to first tenant to get scoped tokens
let currentTenant: Tenant | null = null;
if (tenants.length > 0 && tenants[0]) {
const response = await authService.switchTenant(tenants[0].id);
currentTenant = response.tenant;
}
set({
user,
isAuthenticated: true,
isLoading: false,
availableTenants: tenants,
currentTenant,
userRole: currentTenant?.role ?? 'viewer',
});
} catch (error) {
set({ isLoading: false });
throw error;
}
},
logout: async () => {
try {
await authService.logout();
} finally {
set({
user: null,
isAuthenticated: false,
currentTenant: null,
availableTenants: [],
userRole: 'viewer',
});
}
},
checkAuth: async () => {
const token = getAccessToken();
if (!token) {
set({ isInitializing: false, isAuthenticated: false });
return;
}
try {
const { user, tenants } = await fetchUserAndTenants();
const { currentTenant: persisted } = get();
// Use persisted tenant if still valid, else first tenant
let tenantToSwitch = persisted && tenants.find(t => t.id === persisted.id)
? persisted
: tenants[0] ?? null;
if (tenantToSwitch) {
const response = await authService.switchTenant(tenantToSwitch.id);
tenantToSwitch = response.tenant;
}
set({
user,
isAuthenticated: true,
isInitializing: false,
availableTenants: tenants,
currentTenant: tenantToSwitch,
userRole: tenantToSwitch?.role ?? 'viewer',
});
} catch (error) {
// Keep persisted state on network errors, clear on auth errors
const isAuthError = error.status === 401;
if (isAuthError) {
set({ ...initialState, isInitializing: false });
} else {
set({ isInitializing: false });
}
}
},
switchTenant: async (tenantId: string) => {
const { availableTenants } = get();
const tenant = availableTenants.find(t => t.id === tenantId);
if (!tenant) return;
try {
const response = await authService.switchTenant(tenantId);
set({
currentTenant: response.tenant,
userRole: response.tenant.role,
});
} catch (error) {
console.error('Failed to switch tenant:', error);
// Fallback to local state even if API fails
set({ currentTenant: tenant, userRole: tenant.role });
}
},
updateProfile: async (data) => {
const updatedUser = await authService.updateProfile(data);
set({ user: updatedUser });
return updatedUser;
},
}),
{
name: 'app-auth-storage',
partialize: (state) => ({
user: state.user,
isAuthenticated: state.isAuthenticated,
currentTenant: state.currentTenant,
availableTenants: state.availableTenants,
userRole: state.userRole,
}),
}
)
);
// Selectors
export const selectUser = (state: AuthState) => state.user;
export const selectIsAuthenticated = (state: AuthState) => state.isAuthenticated;
export const selectIsInitializing = (state: AuthState) => state.isInitializing;
export const selectCurrentTenant = (state: AuthState) => state.currentTenant;
export const selectUserRole = (state: AuthState) => state.userRole;
export default useAuthStore;
```
```typescript
// stores/videoStore.ts
import { create } from 'zustand';
import { videoService } from '@/lib/video';
import type { Video, VideoStatus } from '@/types';
interface VideoState {
currentVideo: Video | null;
isLoading: boolean;
isGenerating: boolean;
isPolling: boolean;
error: string | null;
pollingInterval: ReturnType<typeof setInterval> | null;
loadVideo: (videoId: string) => Promise<void>;
generateVideo: (documentId: string) => Promise<void>;
startPolling: (videoId: string, onComplete?: () => void) => void;
stopPolling: () => void;
clearVideo: () => void;
}
const POLLING_INTERVAL = 3000;
export const useVideoStore = create<VideoState>()((set, get) => ({
currentVideo: null,
isLoading: false,
isGenerating: false,
isPolling: false,
error: null,
pollingInterval: null,
loadVideo: async (videoId: string) => {
set({ isLoading: true, error: null });
try {
const video = await videoService.getVideo(videoId);
set({ currentVideo: video, isLoading: false });
} catch (error) {
set({
error: error instanceof Error ? error.message : 'Failed to load video',
isLoading: false,
});
throw error;
}
},
generateVideo: async (documentId: string) => {
set({ isGenerating: true, error: null });
try {
const response = await videoService.generateVideo(documentId);
const video: Video = {
id: response.id,
document_id: documentId,
status: 'queued',
progress_percent: 0,
// ... other fields
};
set({ currentVideo: video, isGenerating: false });
// Start polling for status updates
get().startPolling(response.id);
} catch (error) {
set({
error: error instanceof Error ? error.message : 'Failed to generate video',
isGenerating: false,
});
throw error;
}
},
startPolling: (videoId: string, onComplete?: () => void) => {
// Stop any existing polling
get().stopPolling();
set({ isPolling: true });
const interval = setInterval(async () => {
try {
const status = await videoService.getStatus(videoId);
set((state) => ({
currentVideo: state.currentVideo
? {
...state.currentVideo,
status: status.status,
progress_percent: status.progress_percent,
video_url: status.video_url,
}
: null,
}));
// Stop polling if complete or failed
if (status.status === 'ready' || status.status === 'failed') {
get().stopPolling();
if (onComplete) onComplete();
}
} catch {
// Continue polling on error
}
}, POLLING_INTERVAL);
set({ pollingInterval: interval });
},
stopPolling: () => {
const { pollingInterval } = get();
if (pollingInterval) {
clearInterval(pollingInterval);
}
set({ isPolling: false, pollingInterval: null });
},
clearVideo: () => {
get().stopPolling();
set({ currentVideo: null, error: null });
},
}));
export const selectCurrentVideo = (state: VideoState) => state.currentVideo;
export const selectVideoIsGenerating = (state: VideoState) => state.isGenerating;
export const selectVideoIsPolling = (state: VideoState) => state.isPolling;
export const selectVideoProgress = (state: VideoState) =>
state.currentVideo?.progress_percent ?? 0;
```
```typescript
// HMR persistence wrapper (dev-only pattern)
import { StateCreator } from 'zustand';
const persistHMR =
<T>(name: string, config: StateCreator<T>): StateCreator<T> =>
(set, get, api) => {
// @ts-ignore - Vite HMR API
if (import.meta.hot) {
// @ts-ignore
const existingState = import.meta.hot.data?.[name];
if (existingState) {
setTimeout(() => set(existingState), 0);
}
// @ts-ignore
import.meta.hot.dispose(() => {
// @ts-ignore
import.meta.hot.data[name] = get();
});
}
return config(set, get, api);
};
// Usage:
export const useDevStore = create<DevState>()(
persistHMR('devStore', (set, get) => ({
// store implementation
}))
);
```
## Anti-patterns to avoid
1. **Mutating state directly**: always use `set()` with immutable updates via spread operators, never `state.items.push()` or `state.user.name = 'x'`.
2. **Missing error handling in async actions**: always wrap API calls in try/catch and set `error` state. Reset `isLoading` in finally block or catch.
3. **Not cleaning up polling intervals**: always call `clearInterval()` in `stopPolling()` and on unmount, or you'll leak memory and make redundant API calls.
4. **Persisting everything to storage**: use `partialize` to exclude transient UI state, error messages, loading flags, and large objects. Only persist auth tokens, user preferences, and selected filters.
5. **Over-selecting state in components**: instead of `const state = useAuthStore()` (re-renders on any state change), use `const user = useAuthStore(selectUser)` (re-renders only when user changes).
6. **Hardcoding API URLs in stores**: import service functions from `@/lib/*` that read from environment config, never hardcode `fetch('https://...')`.
7. **Not handling network errors gracefully in session restoration**: if `checkAuth()` fails due to network issues, keep the persisted session instead of logging the user out.
8. **Calling async actions multiple times without guards**: check `if (isLoading || jobId) return;` at the start of async actions to prevent duplicate concurrent calls.
9. **Storing derived state**: if a value can be computed from existing state, use a selector instead of duplicating it in state. Example: `selectHasUnreadMessages = (state) => state.messages.some(m => !m.read)`.
10. **Not typing the store**: always use `create<MyState>()` with the full interface as the type parameter to get type safety in actions and selectors.
## References
- [repo-evidence.md](references/repo-evidence.md) — genericized source snippets
- [store-structure-and-selectors.md](references/store-structure-and-selectors.md) — typed stores, StateCreator slices, exported selectors
- [async-polling-and-persistence.md](references/async-polling-and-persistence.md) — async actions, polling patterns, HMR persistence, devtools/persist middleware
- See also: [frontend-repo-architecture](../frontend-repo-architecture/SKILL.md) for overall project structure
Pliki w paczce (4)
- SKILL.md 17 KB
- references/async-polling-and-persistence.md 13 KB
- references/repo-evidence.md 17 KB
- references/store-structure-and-selectors.md 7 KB
Wersje
W bazie nie ma innych skilli o nazwie zustand-state-patterns.
Identyczna treść występuje jeszcze w 1 innych folderach na GitHubie. Tu pokazujemy jedną, najlepiej ocenioną kopię.
Migawka bazy z 13.11.2026 · wersja paczki b50adde0b103 (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 4 · ranking
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 95zustand-store-patterns
Pomaga tworzyć i zarządzać store'ami Zustand w React: tworzenie, selektory, akcje i podstawowe wzorce użycia. - 95zustand-typescript
Wspiera pracę z Zustand w TypeScript: typowane tworzenie store'ów, typowane selektory i wzorce typów. - 100zustand
Pomaga tworzyć i używać stanów Zustand w TypeScript w projektach Next.js, z selektorami i middleware. - 90nextjs-zustand
Wdraża stan globalny, magazyny, persist i hydrację w komponentach klienckich Next.js 16 App Router z Zustand v5. - 90react-state
Obsługuje zarządzanie stanem w React za pomocą Zustand v5: stan globalny, store'y, persist i stan po stronie klienta. - 100zustand-state-management
Podaje dobre praktyki Zustand w React i TypeScript: projekt store, selektory, middleware, SSR i testy oraz decyzje, czy stan należy do Zustand, komponentu czy biblioteki stanu serwera.