XState v6 alpha
Create actors
Create and start an actor from actor logic.
createActor(...) creates an actor from actor logic, such as a state machine.
import { createActor, createMachine } from 'xstate';
const machine = createMachine({
initial: 'active',
states: { active: {} }
});
const actor = createActor(machine);
actor.start();Subscribe before start() when the subscriber must receive the initial snapshot. An actor does not process events until it starts.
actor.subscribe((snapshot) => console.log(snapshot.value));
actor.start();
actor.send({ type: 'next' });Create a separate actor for each running instance. The same machine can run one actor per browser tab, order or uploaded file.
Actor methods
| Method | Purpose |
|---|---|
start() | Start the actor. |
stop() | Stop the actor. |
send(event) | Send an event. |
subscribe(observer) | Observe snapshots. |
getSnapshot() | Read the current snapshot. |
getPersistedSnapshot() | Get data for persistence. |
Common actor options include id, input, snapshot, inspect and clock.
TypeScript
Use ActorRefFrom to get the actor reference type for actor logic.
import type { ActorRefFrom } from 'xstate';
type MachineActor = ActorRefFrom<typeof machine>;Create actor cheatsheet
const actor = createActor(logic, { input, snapshot, inspect });
actor.subscribe((snapshot) => console.log(snapshot));
actor.start();
actor.send({ type: 'next' });
actor.stop();