Statechart libraries
Read before using a statechart library the project already has. The transition stays pure, effects come back as data, and the actor acts on them; if the library has no call that returns effects as data, use a plain transition function instead of its interpreter. Check every API name against the installed version.
XState v5
The exported transition exists since 5.19. On older versions, or if a name
below is missing, use a plain transition function.
const machine = setup({ types, guards, actions }).createMachine({ ... });
// First write, no row yet: runs the context factory; entry actions come back
// in firstActions, not executed.
const [first, firstActions] = initialTransition(machine, input);
// Later writes: rebuild from the stored value and the full stored context.
const snapshot = machine.resolveState({ value, context });
if (!snapshot.can(event)) {
return rejected(); // no transition, or a guard said no
}
const [next, actions] = transition(machine, snapshot, event);transitionandinitialTransitionrun no action implementations;assignis already applied. TranslatefirstActionslikeactions, in the insert. If the creating event is itself a transition, checkfirst.can(event)and runtransition(machine, first, event)before inserting, then insertnextwith both action lists.- Only named actions with serializable
paramsbecome effects, mapped bytypeandparams.xstate.*entries (raise, log, emit, send, cancel, spawn, stop), anonymous actions or unserializable params put the machine outside this recipe. An immediatexstate.raiseis already applied; replaying it doubles the event. - XState has no rejected result. Decide with
snapshot.can(event), which runs the guards, so keep them pure. It cannot tell "no transition" from "guard failed"; check the state's transitions if callers need both. Comparing state values misses a targetless transition that changes only context. - Persist
next.valueand the wholenext.context;resolveStatewith a partial context leaves fields undefined. Context and effectparamsmust survive the round trip through storage (JSON turns aDateinto a string): decode them to their types, and test the round trip. - Sound only without history states, invoked or spawned actors,
afterdelays, delayedraise,sendTo,emitand inline actions:resolveStaterebuilds with no history or children, and a pure transition never fires timers, so the process stalls with no error. Model delays as a persisted due time plus a scheduler event and external work as an awaiting state; otherwise ask the user. - Use the exported
transition(machine, snapshot, event), notmachine.transition. Never callcreateActororactor.start.