XState v5 Skill
CRITICAL: This skill covers XState v5 ONLY. Do not use v4 patterns, APIs, or documentation. XState v5 requires TypeScript 5.0+.
When to Use
- State machine and statechart design
- Actor system implementation
- XState v5 API usage (
setup, createMachine, createActor)
- Framework integration (React, Vue, Svelte)
- Complex async flow orchestration
Key Concepts
Actors are independent entities that communicate by sending messages. XState v5 supports:
| Actor Type |
Creator |
Use Case |
| State Machine |
createMachine() |
Complex state logic with transitions |
| Promise |
fromPromise() |
Async operations (fetch, timers) |
| Callback |
fromCallback() |
Bidirectional streams (WebSocket, EventSource) |
| Observable |
fromObservable() |
RxJS streams |
| Transition |
fromTransition() |
Reducer-like state updates |
Quick Start
import { setup, assign, createActor } from 'xstate';
const machine = setup({
types: {
context: {} as { count: number },
events: {} as { type: 'increment' } | { type: 'decrement' },
},
actions: {
increment: assign({ count: ({ context }) => context.count + 1 }),
decrement: assign({ count: ({ context }) => context.count - 1 }),
},
}).createMachine({
id: 'counter',
initial: 'active',
context: { count: 0 },
states: {
active: {
on: {
increment: { actions: 'increment' },
decrement: { actions: 'decrement' },
},
},
},
});
// Create and start actor
const actor = createActor(machine);
actor.subscribe((snapshot) => console.log(snapshot.context.count));
actor.start();
actor.send({ type: 'increment' });
v5 API Changes (NEVER use v4 patterns)
| v4 (WRONG) |
v5 (CORRECT) |
createMachine() alone |
setup().createMachine() |
interpret() |
createActor() |
service.start() |
actor.start() |
state.matches() |
snapshot.matches() |
services: {} |
actors: {} |
state.context |
snapshot.context |
Invoke vs Spawn
- invoke: Actor lifecycle tied to state (created on entry, stopped on exit)
- spawn: Dynamic actors independent of state transitions
Inspection API (Debugging)
const actor = createActor(machine, {
inspect: (event) => {
if (event.type === '@xstate.snapshot') {
console.log(event.snapshot);
}
},
});
Event types: @xstate.actor, @xstate.event, @xstate.snapshot, @xstate.microstep
File Organization
feature/
├── feature.machine.ts # Machine definition
├── feature.types.ts # Shared types (optional)
├── feature.tsx # React component
└── feature.test.ts # Machine tests
Learning Path
| Level |
Focus |
| Beginner |
Counter, toggle machines; setup() pattern |
| Intermediate |
Guards, actions, hierarchical states, fromPromise() |
| Advanced |
Observable actors, spawning, actor orchestration |
Supporting Documentation
- PATTERNS.md - Guards, actions, actors, hierarchical/parallel states
- REACT.md - React hooks (
useMachine, useActor, useSelector)
- EXAMPLES.md - Complete working examples
Resources
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: xstate3description: Helps create XState v5 state machines in TypeScript and React. Use when building state machines, actors, statecharts, finite state logic, actor systems, or integrating XState with React/Vue/Svelte components. Use when this capability is needed.4---56# XState v5 Skill78> **CRITICAL: This skill covers XState v5 ONLY.** Do not use v4 patterns, APIs, or documentation. XState v5 requires **TypeScript 5.0+**.910## When to Use1112- State machine and statechart design13- Actor system implementation14- XState v5 API usage (`setup`, `createMachine`, `createActor`)15- Framework integration (React, Vue, Svelte)16- Complex async flow orchestration1718## Key Concepts1920**Actors** are independent entities that communicate by sending messages. XState v5 supports:2122| Actor Type | Creator | Use Case |23|------------|---------|----------|24| State Machine | `createMachine()` | Complex state logic with transitions |25| Promise | `fromPromise()` | Async operations (fetch, timers) |26| Callback | `fromCallback()` | Bidirectional streams (WebSocket, EventSource) |27| Observable | `fromObservable()` | RxJS streams |28| Transition | `fromTransition()` | Reducer-like state updates |2930## Quick Start3132```typescript33import { setup, assign, createActor } from 'xstate';3435const machine = setup({36 types: {37 context: {} as { count: number },38 events: {} as { type: 'increment' } | { type: 'decrement' },39 },40 actions: {41 increment: assign({ count: ({ context }) => context.count + 1 }),42 decrement: assign({ count: ({ context }) => context.count - 1 }),43 },44}).createMachine({45 id: 'counter',46 initial: 'active',47 context: { count: 0 },48 states: {49 active: {50 on: {51 increment: { actions: 'increment' },52 decrement: { actions: 'decrement' },53 },54 },55 },56});5758// Create and start actor59const actor = createActor(machine);60actor.subscribe((snapshot) => console.log(snapshot.context.count));61actor.start();62actor.send({ type: 'increment' });63```6465## v5 API Changes (NEVER use v4 patterns)6667| v4 (WRONG) | v5 (CORRECT) |68|------------|--------------|69| `createMachine()` alone | `setup().createMachine()` |70| `interpret()` | `createActor()` |71| `service.start()` | `actor.start()` |72| `state.matches()` | `snapshot.matches()` |73| `services: {}` | `actors: {}` |74| `state.context` | `snapshot.context` |7576## Invoke vs Spawn7778- **invoke**: Actor lifecycle tied to state (created on entry, stopped on exit)79- **spawn**: Dynamic actors independent of state transitions8081## Inspection API (Debugging)8283```typescript84const actor = createActor(machine, {85 inspect: (event) => {86 if (event.type === '@xstate.snapshot') {87 console.log(event.snapshot);88 }89 },90});91```9293Event types: `@xstate.actor`, `@xstate.event`, `@xstate.snapshot`, `@xstate.microstep`9495## File Organization9697```98feature/99├── feature.machine.ts # Machine definition100├── feature.types.ts # Shared types (optional)101├── feature.tsx # React component102└── feature.test.ts # Machine tests103```104105## Learning Path106107| Level | Focus |108|-------|-------|109| Beginner | Counter, toggle machines; `setup()` pattern |110| Intermediate | Guards, actions, hierarchical states, `fromPromise()` |111| Advanced | Observable actors, spawning, actor orchestration |112113## Supporting Documentation114115- [PATTERNS.md](PATTERNS.md) - Guards, actions, actors, hierarchical/parallel states116- [REACT.md](REACT.md) - React hooks (`useMachine`, `useActor`, `useSelector`)117- [EXAMPLES.md](EXAMPLES.md) - Complete working examples118119## Resources120121- [Official Docs](https://stately.ai/docs/xstate)122- [Stately Studio](https://stately.ai/studio) - Visual editor123124---125> Converted and distributed by [TomeVault](https://tomevault.io/claim/seed-hypermedia) — claim your Tome and manage your conversions.126<!-- tomevault:4.0:skill_md:2026-04-11 -->