Actors in XState
Learn all about using actors in XState.
Introducing actors
When you run a statechart, it becomes an actor, a running process that can receive events. Often, you’ll need your actor to run other actors.
Promises
The most common type of actors you’ll invoke are promise actors. Promise actors allow you to await the result of a promise before deciding what to do next.
Actions vs. actors
Sometimes it’s unclear whether you should use an action or an actor. Both appear to do similar things, executing side effects. Let’s break down the differences.
Callbacks
Promise actors let you model promises, but not every actor will be a promise. Callback actors give you a flexible API for managing a long-running actor.
Machines
Machine actors enable you to create machines that can act as reusable modules across your application. You can pass a machine directly to the invoke src.
Observables
Observables are streams of values emitted over time. Observables can be invoked, sending events to the parent machine.
Parent to child communication
Invoked actors can send events to their parent using the sendParent and sendBack methods. Child actors can also receive events from the parent.
Spawning actors
You can use spawn to run actors. Actors created with spawn are spawning actors, and actors created with invoke are invoking actors.
Actor cheatsheet
Get working quickly with actors using our quick reference XState cheatsheet, including spawning, sending, and receiving.