xstate
Model front-end and workflow logic as XState v5 statecharts and actors — when finite states earn their place (and when @xstate/store or a discriminated union…
by citypaul★ 764 GitHub stars
Install xstate in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
XState
State machines and statecharts (XState v5) turn front-end flow logic from scattered event handlers into one explicit, verifiable model. This skill exists because that trade is unusually good in AI-assisted development:
- Headless testability — a machine is events in, snapshots out. Flow logic is proven with fast unit tests before any UI exists, and
xstate/graphcan generate covering paths through every transition. - Impossible states are unrepresentable — finite states plus guarded transitions delete the boolean-flag bug class (
isLoading && isError); the model forbids drift instead of relying on discipline. - One reviewable artifact — every state × event combination lives in a single declarative structure, so agents and reviewers reason about the whole behavior surface at once.
- Humans get the diagram for free — Stately Studio and the inspector render exactly the machine that ships; review becomes "look at the chart".
Default stance: model front-end flow logic — wizards, checkout, auth, uploads, anything with modes, sequencing, cancellation, timeouts, retries — as statecharts. Keep trivial state honest: data-shaped state with no modes goes to @xstate/store or a plain discriminated union, never a ceremonial machine.
The failure mode this skill exists to catch is under-modeling, not over-modeling. Model training data is overwhelmingly ordinary React — useState and useEffect for everything — so the realistic risk is quietly hand-rolling a statechart, not building a ceremonial one. Read Before You Write State before reaching for useState, not after deciding to use XState.
Version guardrail: XState v5 only (setup(), createActor, actors). Most tutorials, old blog posts, and model training data are v4 — if you see Machine(), interpret(), cond:, services:, or typegen, you are looking at v4; translate via the migration table before any of it ships.
Load alongside: testing (behavior-driven patterns and factories), react-testing/front-end-testing (component-level tests), typescript-strict (schema-first at trust boundaries — external events get parsed before send), functional (context is immutable data). For event-sourced domain state, event-sourcing owns the Decider; an XState machine may drive a UI flow in front of it, but the event log is not machine context.
Read the relevant reference before deciding:
| Reference | Read when... |
|---|---|
references/when-to-model.md |
Deciding machine vs store vs union — the complexity ladder, flag-smell signals, finite-state vs context split, store→machine migration, where machines live |
references/machine-design.md |
Designing a machine — event-first workflow, naming, hierarchy/parallel/history/final, transition and re-entry semantics, actions, guards, delays, anti-pattern catalog |
references/actors-and-systems.md |
Effects and composition — actor logic types, invoke vs spawn, spawn hygiene, communication, systemId receptionist, persistence, inspection |
references/react-integration.md |
Wiring to React — hook selection, createActorContext, StrictMode, @xstate/store, production pitfalls |
references/testing.md |
Testing — the layered split, provide() as the seam, SimulatedClock, pure transition, model-based testing, persistence round-trips, mutation-score guidance |
references/typescript-and-migration.md |
Types and versions — setup() typing model, assertEvent, type helpers, no-typestates caveat, full v4→v5 table |
Before You Write State
This section fires on ordinary React code. You do not have to be in a machine file — or have decided to use XState at all — for it to apply.
Step 0: Find out who owns machines in this repository
Before creating a machine, moving state into one, or deciding where a new actor lives, check what this repository already says: architecture or boundary tests, import-boundary rules such as eslint-plugin-boundaries or dependency-cruiser, architecture decision records, the directory already holding existing machines, CLAUDE.md, or the layout guidance in structure-codebase. Repository convention outranks this skill's default colocation advice.
A correct machine in a forbidden layer still fails the build. Establish the allowed home before writing, not after CI rejects it.
If you just wrote one of these, stop — you are already at step 1
Each of these is a hand-rolled statechart: the states exist, they are just implicit, scattered, and unverified. Treat any single hit as the trigger to work through this skill.
| What you wrote | The statechart hiding in it |
|---|---|
useState for submitting, isLoading, ` |
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
- agents/openai.yaml
- references/actors-and-systems.md
- references/machine-design.md
- references/react-integration.md
- references/testing.md
- references/typescript-and-migration.md
- references/when-to-model.md
FAQ
What does the xstate skill do?
Model front-end and workflow logic as XState v5 statecharts and actors — when finite states earn their place (and when @xstate/store or a discriminated union is honest), event-first machine design, guards/actions/delays, invoke vs spawn, app-level actor systems, persistence, @xstate/react integration, behavior-driven machine testing including model-based path generation via xstate/graph, and a Mermaid stateDiagram-v2 render regenerated whenever a machine is designed or changed. Fires on ordinary React code that is already a hand-rolled statechart, not only on files that import xstate — a submitting or isLoading useState, a then/catch chain that sets state in sequence, a double-submit guard such as an early return while submitting, an error cleared just before a retry, a useEffect needing an ignore flag in its cleanup, a timer something must clear, or a drag or gesture handler binding document listeners it must unbind. Two tests decide ownership, never how the value renders: an external answer changed it, or the interaction has an interruptible middle. A disabled button or spinner is the presentation of temporal state, not presentation state. State stays in React only when both tests come back empty — one event sets it completely, with no phase to interrupt and nothing to dispose. Use when building or reviewing wizards, checkout/auth/upload flows, async orchestration with retries or timeouts, canvas and editor interactions, anything sent to a BFF or server, boolean-flag state bugs, or any XState or state-machine code. For general test patterns see testing; for component tests see react-testing; for store-only state see the when-to-model reference first.
How do I install the xstate skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the xstate skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
docx
AnthropicUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include…
AnthropicUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple…
xlsx
AnthropicUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing…
pptx
AnthropicUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or…