home / skills / sickn33 / antigravity-awesome-skills / zustand-store-ts
This skill helps you create and manage Zustand stores with TypeScript, subscribeWithSelector, and clean state-action separation for React apps.
npx playbooks add skill sickn33/antigravity-awesome-skills --skill zustand-store-tsReview the files below or copy the command above to add this skill to your agents.
---
name: zustand-store-ts
description: 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.
---
# Zustand Store
Create Zustand stores following established patterns with proper TypeScript types and middleware.
## Quick Start
Copy the template from [assets/template.ts](assets/template.ts) and replace placeholders:
- `{{StoreName}}` → PascalCase store name (e.g., `Project`)
- `{{description}}` → Brief description for JSDoc
## Always Use subscribeWithSelector
```typescript
import { create } from 'zustand';
import { subscribeWithSelector } from 'zustand/middleware';
export const useMyStore = create<MyStore>()(
subscribeWithSelector((set, get) => ({
// state and actions
}))
);
```
## Separate State and Actions
```typescript
export interface MyState {
items: Item[];
isLoading: boolean;
}
export interface MyActions {
addItem: (item: Item) => void;
loadItems: () => Promise<void>;
}
export type MyStore = MyState & MyActions;
```
## Use Individual Selectors
```typescript
// Good - only re-renders when `items` changes
const items = useMyStore((state) => state.items);
// Avoid - re-renders on any state change
const { items, isLoading } = useMyStore();
```
## Subscribe Outside React
```typescript
useMyStore.subscribe(
(state) => state.selectedId,
(selectedId) => console.log('Selected:', selectedId)
);
```
## Integration Steps
1. Create store in `src/frontend/src/store/`
2. Export from `src/frontend/src/store/index.ts`
3. Add tests in `src/frontend/src/store/*.test.ts`
This skill generates ready-to-use Zustand stores in TypeScript that follow proven patterns: typed state/action separation, subscribeWithSelector middleware, and per-selector reactivity. It provides a template and guidance to create global or feature stores for React apps, with testing and export conventions included.
The skill scaffolds a TypeScript store template that composes state and actions into a single typed store type, wraps the store with subscribeWithSelector, and enforces individual selectors for minimal re-renders. It also includes examples for subscribing outside React and recommendations for where to place and export stores in a codebase.
Why use subscribeWithSelector?
subscribeWithSelector enables fine-grained selectors and external subscriptions so components and non-UI code only react to relevant state changes, reducing unnecessary re-renders.
How should I structure types for a store?
Declare separate interfaces for State and Actions, then export a combined Store type (State & Actions) so types are explicit and reusable in components and tests.