Create Zustand stores with TypeScript, subscribeWithSelector middleware, and proper state/action separation. Use when building React state management, creating global stores, or implementing reactive state patterns with Zustand.
Add this skill
npx mdskills install sickn33/zustand-store-tsClear TypeScript patterns for Zustand stores with middleware and best practices
1---2name: zustand-store-ts3description: Create Zustand stores with TypeScript, subscribeWithSelector middleware, and proper state/action separation. Use when building React state management, creating global stores, or implementing reactive state patterns with Zustand.4---56# Zustand Store78Create Zustand stores following established patterns with proper TypeScript types and middleware.910## Quick Start1112Copy the template from [assets/template.ts](assets/template.ts) and replace placeholders:13- `{{StoreName}}` → PascalCase store name (e.g., `Project`)14- `{{description}}` → Brief description for JSDoc1516## Always Use subscribeWithSelector1718```typescript19import { create } from 'zustand';20import { subscribeWithSelector } from 'zustand/middleware';2122export const useMyStore = create<MyStore>()(23 subscribeWithSelector((set, get) => ({24 // state and actions25 }))26);27```2829## Separate State and Actions3031```typescript32export interface MyState {33 items: Item[];34 isLoading: boolean;35}3637export interface MyActions {38 addItem: (item: Item) => void;39 loadItems: () => Promise<void>;40}4142export type MyStore = MyState & MyActions;43```4445## Use Individual Selectors4647```typescript48// Good - only re-renders when `items` changes49const items = useMyStore((state) => state.items);5051// Avoid - re-renders on any state change52const { items, isLoading } = useMyStore();53```5455## Subscribe Outside React5657```typescript58useMyStore.subscribe(59 (state) => state.selectedId,60 (selectedId) => console.log('Selected:', selectedId)61);62```6364## Integration Steps65661. Create store in `src/frontend/src/store/`672. Export from `src/frontend/src/store/index.ts`683. Add tests in `src/frontend/src/store/*.test.ts`69
Full transparency — inspect the skill content before installing.