AgentSkillsCN

xstate

帮助您在 TypeScript 与 React 中构建 XState v5 状态机。当您需要构建状态机、角色、状态图、有限状态逻辑、角色系统,或希望将 XState 与 React/Vue/Svelte 组件无缝集成时,此技能将助您事半功倍。

SKILL.md
--- frontmatter
name: xstate
description: 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.
user-invocable: false

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 TypeCreatorUse Case
State MachinecreateMachine()Complex state logic with transitions
PromisefromPromise()Async operations (fetch, timers)
CallbackfromCallback()Bidirectional streams (WebSocket, EventSource)
ObservablefromObservable()RxJS streams
TransitionfromTransition()Reducer-like state updates

Quick Start

typescript
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() alonesetup().createMachine()
interpret()createActor()
service.start()actor.start()
state.matches()snapshot.matches()
services: {}actors: {}
state.contextsnapshot.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)

typescript
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

code
feature/
├── feature.machine.ts    # Machine definition
├── feature.types.ts      # Shared types (optional)
├── feature.tsx           # React component
└── feature.test.ts       # Machine tests

Learning Path

LevelFocus
BeginnerCounter, toggle machines; setup() pattern
IntermediateGuards, actions, hierarchical states, fromPromise()
AdvancedObservable 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