superpower-zustand
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
What this skill does
# Zustand StoreBuilder Pattern
<CRITICAL>
DO NOT create Zustand stores using standard patterns (create with inline actions). ALL Zustand stores in this project MUST use the StoreBuilder pattern defined below. This is a required architectural standard, not a suggestion.
</CRITICAL>
## Purpose
Enforce a standardized, type-safe approach to creating Zustand stores that:
- Separates state definition from actions using the factory pattern
- Integrates immer middleware for convenient immutable updates
- Supports optional persistence with fine-grained control
- Exposes both reactive (useStore hook) and non-reactive (get/set) access
- Maintains consistent patterns across the codebase
## When to Use This Skill
Use this skill when:
- Creating new Zustand stores for state management
- User requests state management solutions in a React application
- Implementing stores for any feature requiring client-side state
## Required Pattern
All Zustand stores MUST use the StoreBuilder utility located in `assets/storebuilder.ts`.
### Core Implementation Steps
1. **Copy the StoreBuilder utility** (if not already in the project)
- Source: `skills/superpower-zustand/assets/storebuilder.ts`
- Destination: `src/lib/storebuilder.ts` (or similar location in the project)
2. **Define state type separately from actions**
- Create a type for the full store (state + actions)
- Use `Omit` to exclude action methods when passing to StoreBuilder
3. **Initialize the store with StoreBuilder**
- Pass initial state as first argument
- Optionally pass PersistConfig as second argument for persistence
4. **Separate actions using createFactory**
- Define all actions as methods in the createFactory argument
- Actions access `set` from the StoreBuilder closure
- Use immer-style mutations within `set` callbacks
5. **Export the factory-created hook**
- The hook returned by createFactory combines state, actions, and store utilities
### Required Code Structure
```typescript
import { 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 };
```
### State Updates with Immer
When using `set`, write mutations directly on the draft state (immer middleware is included):
```typescript
// ✅ 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 }));
```
### Persistence Configuration
When state should persist across sessions:
```typescript
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,
}),
}
);
```
## Reference Documentation
For detailed examples and advanced patterns, read `references/pattern-guide.md`:
- Basic usage examples
- Persistence patterns
- Complex stores with async actions
- Using get/set outside React components
- Type safety patterns
Load the reference documentation when:
- Implementing complex stores with async operations
- Needing examples of persistence configuration
- User asks about advanced Zustand patterns
- Unsure about specific implementation details
## Verification
After creating a store, verify:
1. ✅ StoreBuilder utility is imported from project location
2. ✅ State type uses `Omit` to exclude actions
3. ✅ All actions are defined in `createFactory`, not in initial state
4. ✅ State updates use immer-style mutations (mutate draft, don't return new object)
5. ✅ Exported hook name follows convention (e.g., `useMyStore`)
6. ✅ Persistence config is included if state should persist
## Non-React Usage
The pattern supports non-reactive access outside React components:
```typescript
const { 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));
```
Use `get` and `set` when:
- Accessing state in utility functions
- Implementing middleware or side effects
- Working outside React component lifecycle
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.