Imported from statelyai/skills (
skills/state-management/SKILL.md). Install upstream withnpx skills add statelyai/skills --skill state-management. Copyright stays with the author.
State management
Choose the state model before the library. For existing code, check the workspace dependencies and trace the reads and writes in the affected flow. For a new design, start with the domain events and constraints.
Model the flow
- Inventory each value, who writes it, who reads it, and how long it lives.
- Keep one authoritative copy of each fact. Compute derived values in a selector, getter, or render. Store an additional copy only when a cache boundary needs one.
- List the combinations the domain allows. Use a single status for mutually exclusive modes. If a mode owns specific data, give it a discriminated union member.
- List the domain events and their payloads. For each event, record the modes that accept it, the next state, and any effects. Run effects at the boundary, such as a request started by a submit event.
- Choose the simplest representation that enforces the invariants. Use local state for independent values, a reducer for coordinated updates, and a machine or actors for workflows with retries, cancellation, or concurrent processes. Normalize related entities when nested updates can conflict.
- Assign ownership using the table below. Map the model to the target library when the task calls for implementation.
| Value | Owner |
|---|---|
| Transient UI state | Closest component that owns it |
| Shared client state | Common owner, reducer, or store |
| Shareable navigation or filters | URL |
| Remote data and freshness | Server cache or query layer |
| Timers, subscriptions, and DOM handles | Ref or external mutable object |
| Undo or audit history | Explicit log or stack at the owning boundary |
Represent mutually exclusive modes
With separate flags, a failed upload can also appear complete. Give each status the data its consumers need.
type UploadFlags = {
isUploading: boolean;
isComplete: boolean;
error: string | null;
url: string | null;
};
const conflicting: UploadFlags = {
isUploading: false,
isComplete: true,
error: 'Connection lost',
url: null,
};
type Upload =
| { status: 'idle' }
| { status: 'uploading' }
| { status: 'complete'; url: string }
| { status: 'failed'; error: string };
const canRetry = (upload: Upload) => upload.status === 'failed';
A successful upload now requires a URL. Retry availability comes from the status.
Verify the model
For a design, walk a normal path and a failing or out-of-order path through the event and transition list. For a refactor, run the affected tests or replay those paths through the existing entry point. Check the observable state and effects against the invariants. If a path fails, revise the event, transition, or owner that caused the mismatch, then rerun the same path. Report any path you could only check by inspection.
References
Open the reference for the current task.
- React
- Redux and Redux Toolkit
- Zustand
- Vue and Pinia
- Plain JavaScript
- TypeScript modeling and exhaustiveness
- Async lifecycle modeling
- Review checklist
For XState implementation, use the XState skill that matches the installed major version.
Present the result
For a review, lead with each concrete bug or invalid state and its consequence. For a design, lead with the invariants and proposed model. Include source and derived values, valid events, and ownership. Show code when the task calls for implementation.
