Zustand Store TS
MicrosoftOfficialCreated by Microsoft
- Skill
- Tool integration
- Software development
- MIT
- Low risk
- Has scripts
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a standardized approach to building global state management in React applications using Zustand. It emphasizes TypeScript integration, the use of the subscribeWithSelector middleware, and the architectural separation of state and actions to ensure clean, maintainable, and performant code.
What it does
- Defines patterns for separating state and action interfaces in TypeScript.
- Demonstrates the implementation of the subscribeWithSelector middleware.
- Provides a template for consistent store creation.
- Encourages the use of individual selectors to prevent unnecessary component re-renders.
- Shows how to implement external subscriptions for non-React code.
When to use it
- When setting up global state management for a React application.
- When you need to ensure type safety for complex state objects.
- When you want to optimize performance by avoiding broad state subscriptions.
- When implementing reactive state patterns that require external subscriptions.
Advantages
- Promotes consistent store architecture across the project.
- Improves performance by minimizing unnecessary re-renders.
- Enhances code maintainability through clear separation of concerns.
- Ensures robust type checking for state and actions.
Usage examples
- “Create a new Zustand store for managing user authentication state.”
- The agent generates a TypeScript file using the defined interfaces for UserState and UserActions, wrapped in the subscribeWithSelector middleware.
- “How should I select data from my Zustand store to avoid re-rendering?”
- The agent provides a code snippet demonstrating the use of individual selectors, such as const data = useStore((state) => state.data), instead of destructuring the entire store.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/microsoft/zustand-store-ts/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip zustand-store-ts.zip -d .agents/skills
Documentation by Microsoft
Zustand Store
Create Zustand stores following established patterns with proper TypeScript types and middleware.
Quick Start
Copy the template from assets/template.ts and replace placeholders:
{{StoreName}}→ PascalCase store name (e.g.,Project){{description}}→ Brief description for JSDoc
Always Use subscribeWithSelector
import { create } from 'zustand';
import { subscribeWithSelector } from 'zustand/middleware';
export const useMyStore = create<MyStore>()(
subscribeWithSelector((set, get) => ({
// state and actions
}))
);Separate State and Actions
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
// 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
useMyStore.subscribe(
(state) => state.selectedId,
(selectedId) => console.log('Selected:', selectedId)
);Integration Steps
Create store in
src/frontend/src/store/Export from
src/frontend/src/store/index.tsAdd tests in
src/frontend/src/store/*.test.ts
Files
- ATTRIBUTION.mdAdded by AutoGineer967 B
- LICENSELicence file added by AutoGineer1.1 KiB
- SKILL.md1.7 KiB
- assets/template.tsScript3.3 KiB
assets/template.ts
import { create } from 'zustand';
import { subscribeWithSelector } from 'zustand/middleware';
// ============================================================================
// Types
// ============================================================================
/**
* {{StoreName}}State - The state shape for this store
*/
export interface {{StoreName}}State {
// Add state properties
items: unknown[];
selectedId: string | null;
isLoading: boolean;
error: string | null;
}
/**
* {{StoreName}}Actions - Actions that can be performed on this store
*/
export interface {{StoreName}}Actions {
// Setters
setItems: (items: unknown[]) => void;
setSelectedId: (id: string | null) => void;
setLoading: (loading: boolean) => void;
setError: (error: string | null) => void;
// Complex actions
loadItems: () => Promise<void>;
addItem: (item: unknown) => void;
removeItem: (id: string) => void;
// Reset
reset: () => void;
}
/**
* {{StoreName}}Store - Combined store type
*/
export type {{StoreName}}Store = {{StoreName}}State & {{StoreName}}Actions;
// ============================================================================
// Initial State
// ============================================================================
const initialState: {{StoreName}}State = {
items: [],
selectedId: null,
isLoading: false,
error: null,
};
// ============================================================================
// Store
// ============================================================================
/**
* use{{StoreName}}Store - Zustand store for managing {{description}}
*
* @example
* ```typescript
* // In a component - use individual selectors for performance
* const items = use{{StoreName}}Store((state) => state.items);
* const loadItems = use{{StoreName}}Store((state) => state.loadItems);
*
* // Subscribe to changes outside React
* use{{StoreName}}Store.subscribe(
* (state) => state.selectedId,
* (selectedId) => console.log('Selected:', selectedId)
* );
* ```
*/
export const use{{StoreName}}Store = create<{{StoreName}}Store>()(
subscribeWithSelector((set, get) => ({
// Initial state
...initialState,
// Simple setters
setItems: (items) => set({ items }),
setSelectedId: (selectedId) => set({ selectedId }),
setLoading: (isLoading) => set({ isLoading }),
setError: (error) => set({ error }),
// Async action example
loadItems: async () => {
set({ isLoading: true, error: null });
try {
// const items = await fetchItems();
const items: unknown[] = []; // Replace with actual fetch
set({ items, isLoading: false });
} catch (error) {
set({
error: error instanceof Error ? error.message : 'Failed to load',
isLoading: false,
});
}
},
// Add item (immutable update)
addItem: (item) => {
set({ items: [...get().items, item] });
},
// Remove item (immutable update)
removeItem: (id) => {
set({
items: get().items.filter((item) => (item as { id: string }).id !== id),
// Clear selection if removed item was selected
selectedId: get().selectedId === id ? null : get().selectedId,
});
},
// Reset to initial state
reset: () => set(initialState),
}))
);
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Node.js
Security
What our scanner noticed· Low risk
- Scripts that your agent may run
This skill includes scripts that your agent may run. Read them before you use it.
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Microsoft
- Publisher
- Microsoft (Official)
- Source
- microsoft/skills · .github/plugins/azure-sdk-typescript/skills/zustand-store-ts · commit 0696600
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Microsoft.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download zustand-store-ts.zip