What are actors in XState

We are now ready to see the first example of state logic using XState.

Since v5, the library core building block is an actor.

An actor is nothing more that a process that:

  • Contains its own state
  • Interacts with other processes by sending and receiving events

XState provides many kinds of actors. We are going to learn about all of them throughout this course:

  • fromTransition: actor from reducer function
  • fromPromise: actor from async function
  • setup + createMachine: state machine actor
  • fromCallback: actor that can send back events to the parent process
  • fromObservable: actor that represents an observable stream of values

Wait, what about state machines? Isn't XState a state machine library?

State machines are a specific type of actor, but not the only type of actor. In fact, while being the most complete state management abstraction, state machines may not be necessary for every use case.

XState is a full state management and orchestration solution, as such it supports all kinds of use cases. This makes choosing the right solution more complex at the beginning, since there are many ways to implement the same logic.

The suggestion is to always start with the simplest actor. We will follow this advice in this course.

Again, don't worry too much for now. As we explore more about XState we will understand different patterns and learn to choose the best one for each use case.