Loading...
Loading...
MANDATORY for creating Zustand stores. This skill is required when users request state management, creating stores, or mention Zustand. Do NOT create Zustand stores without this skill - all stores must use the required StoreBuilder pattern with immer middleware and factory pattern separation
npx skill4agent add cygnusfear/agent-skills superpower-zustandassets/storebuilder.tsskills/superpower-zustand/assets/storebuilder.tssrc/lib/storebuilder.tsOmitsetsetimport { StoreBuilder } from './storebuilder';
// 1. Define complete state type
type MyStoreState = {
// State fields
value: number;
items: string[];
// Action methods
setValue: (v: number) => void;
addItem: (item: string) => void;
};
// 2. Initialize StoreBuilder with state only (Omit actions)
const { set, createFactory } = StoreBuilder<Omit<MyStoreState, 'setValue' | 'addItem'>>(
{
value: 0,
items: [],
},
// Optional: persistence config
// {
// name: 'my-store',
// version: 1,
// }
);
// 3. Create factory with actions
const useMyStore = createFactory({
setValue: (v: number) => set((state) => { state.value = v; }),
addItem: (item: string) => set((state) => { state.items.push(item); }),
});
// 4. Export the hook
export { useMyStore };set// ✅ Correct: Mutate draft
set((state) => {
state.count += 1;
state.items.push(newItem);
state.nested.property = 'value';
});
// ❌ Incorrect: Don't return new object
set((state) => ({ ...state, count: state.count + 1 }));const { createFactory } = StoreBuilder(
initialState,
{
name: 'storage-key', // Required: localStorage key
version: 1, // Optional: for migration handling
storage: sessionStorage, // Optional: defaults to localStorage
partialize: (state) => ({ // Optional: persist only specific fields
theme: state.theme,
preferences: state.preferences,
}),
}
);references/pattern-guide.mdOmitcreateFactoryuseMyStoreconst { get, set, subscribe } = StoreBuilder(initialState);
// Get current state
const current = get();
// Update state
set((state) => { state.value = 10; });
// Subscribe to changes
const unsubscribe = subscribe((state) => console.log(state));getset