# Finite State Machines & Actors

> Eliminate impossible states by constraining your UI to a finite set of statuses using XState v5.

Subject: State Management | Lesson 17 of 22 | 7 min read
Interactive lesson: https://codewithanimation.in/state-management/xstate-fsm-and-actors
Previous: [The Boolean Explosion](https://codewithanimation.in/state-management/impossible-states-boolean-explosion.md) | Next: [Context, Guards, & Actions](https://codewithanimation.in/state-management/xstate-context-and-actions.md)

## Finite State Machines (FSM)

A Finite State Machine (FSM) is a mathematical model of computation. It describes a system that can be in exactly one of a finite number of 'states' at any given time.

An FSM is defined by its States (modes of existence like `idle` or `loading`), Events (signals or triggers like `FETCH`), and Transitions (the directed movement from one state to another caused by an event).

## Solving the Boolean Explosion

FSMs completely eliminate the Boolean Explosion. By explicitly defining mutually exclusive states (e.g., `'idle' | 'loading' | 'success' | 'error'`), impossible combinations literally cannot exist in memory.

Furthermore, transitions are deterministic. If an event is dispatched but the machine is in a state that doesn't handle that event, the event is safely ignored, preventing illegal mutations.

## The Actor Model (XState v5)

In XState v5, everything is an actor. An actor is an independent, live process that encapsulates its own state and communicates purely by receiving asynchronous messages (events) in its mailbox.

When building FSMs in XState, you first define the logic blueprint using `createMachine`. Then, you instantiate a live, running instance of that logic using `createActor(machine).start()`.

## Common mistake: The Uncoordinated `useState` Problem

When you rely on standard `useState` for complex flows, you are forced to manage transitions imperatively (e.g., remembering to call `setIsLoading(false)`).

FSMs pull this logic out of your components and into a strict, declarative statechart. The UI becomes a pure reflection of the current machine state, completely decoupled from transition logic.

## Tip: Machines vs Actors

Think of `createMachine` like a class definition or an architectural blueprint. It is stateless and inactive.

Think of `createActor` like instantiating an object from that class (`new Machine()`). It is stateful, active, and can actually receive events via `.send()`.

## Try it: Infographic: The Fetch Statechart

Review the statechart diagram below. Notice how the deterministic transitions (arrows) constrain the flow, making bugs like double-submits mathematically impossible.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/xstate-fsm-and-actors

## Check yourself

### Question 1

In XState v5, what is the fundamental difference between `createMachine` and `createActor`?

- A. `createMachine` is used for synchronous logic, while `createActor` is used for async API calls.
- B. `createMachine` defines the stateless logic blueprint, while `createActor` creates the live, running instance that actually receives events.
- C. `createActor` is deprecated in v5, and `createMachine` now handles everything internally.

Correct answer: B

`createMachine` is a pure definition object. You must pass it to `createActor` and call `.start()` to boot up a live actor process with its own state and mailbox.

- Why not A: Both can handle async logic. The difference is blueprint vs live instance.
- Why not C: False. `createActor` is the core API of XState v5.

## Remember this

- A Finite State Machine can only be in exactly one state at a time, eliminating impossible states.
- Transitions are deterministic: events are safely ignored if the current state doesn't handle them.
- XState v5 uses the Actor model for everything.
- `createMachine` creates the logic blueprint; `createActor` creates the live, running instance.
