Stately
XState v6 alpha

Machine configuration

Reference for the top-level state machine configuration.

XState v6 is in alpha

APIs and behavior may change before the stable release.

Create state machine logic with createMachine(...).

const machine = createMachine({
  id: 'request',
  context: { attempts: 0 },
  initial: 'idle',
  states: { idle: {}, loading: {} }
});

Configuration properties

PropertyDescription
idStable identifier for the root state node.
descriptionHuman-readable description.
schemasSchemas for context, events, input and output.
contextInitial context value or initializer.
initialInitial child state.
statesChild state nodes.
onTransitions available in every child state.
entryTransition function run when the machine starts.
exitTransition function run when the machine stops.
invokeActor logic invoked for the machine's lifetime.
outputOutput produced on completion.
actorsActor logic sources.
actionsNamed action sources.
guardsNamed guard sources.
delaysNamed delay sources.

Define implementations with setup(...) when the machine should be fully typed and reusable. Use machine.provide(...) when an application needs to replace an implementation without changing the machine structure.

const testMachine = machine.provide({
  actors: { chargeCard: fakeChargeCard }
});

A media player machine may provide browser audio actors in production and fake actors in tests. An order machine may provide different payment actors for development and production.

TypeScript

createMachine(...) infers state keys and literal transition targets. Use schemas when events or context need explicit types.

Machine cheatsheet

const machine = createMachine({
  id: 'workflow',
  context: {},
  initial: 'idle',
  states: {
    idle: { on: { start: { target: 'active' } } },
    active: { on: { stop: { target: 'idle' } } }
  }
});

On this page