Spawn
XState is based on the actor model. Spawned actors are managed by the state machine.
Coming soon⦠example.
Spawing actors in Stately Studioβ
Coming soon
APIβ
actions: assign({
ref: ({ spawn }) => spawn(fromPromise(...), {
id: 'some-id',
})
})
spawn(actorBehavior, options?)
actorBehavior
- The behavior of the actor to spawn. This can be a function, promise, observable, or callback.options
- Options for spawning the actor.id
- The ID of the actor. This is used to reference the actor in the state machine.input
Sourceβ
- Inline:
spawn(fromPromise(...))
- Referenced:
spawn('getUser')
.provide({ actors })
Lifecycleβ
- Created & started when spawned
- Stopped when the machine is stopped
- Can be manually stopped
Stopping an actorβ
actions: stop('some-id')
Note this does not clear it from the
context
TypeScriptβ
Coming soon
Cheatsheetβ
Coming soon