# Context, Guards, & Actions

> Manage quantitative data alongside finite states using Context, and enforce strict business logic with Guards.

Subject: State Management | Lesson 18 of 22 | 9 min read
Interactive lesson: https://codewithanimation.in/state-management/xstate-context-and-actions
Previous: [Finite State Machines & Actors](https://codewithanimation.in/state-management/xstate-fsm-and-actors.md) | Next: [Atomic State (Jotai)](https://codewithanimation.in/state-management/atomic-state-jotai.md)

## The Extended State

While Finite States are perfect for discrete statuses (`idle`, `loading`), they cannot model continuous quantitative data like an email string, a counter, or a user profile object.

XState solves this with **Context**, also known as the extended state. Context is a strongly typed data object that persists inside the actor, living alongside the active finite state node.

## Preventing State Node Explosion

If you tried to model a user's email input using purely finite states, you would need millions of state nodes (one for every possible string permutation).

Context prevents state explosion. It allows the machine to hold infinite data values without adding any new state nodes to the blueprint.

## Actions and Guards

You update Context using **Actions**. The `assign()` action is a pure, side-effect function that executes synchronously during a transition to immutably merge new data into the context.

You restrict transitions using **Guards**. A guard is a pure predicate function (`(args) => boolean`). If a guard returns `false` during an event, the transition is blocked, preventing the machine from entering an invalid state.

## Common mistake: The Forgotten Cleanup

When managing complex multi-step flows with native React `useState`, it is notoriously easy to forget to clear old context (like a stale error message) when navigating between steps.

Because XState ties actions directly to transitions, you can guarantee that certain state is explicitly reset every time a specific edge is traversed.

## Tip: Guards Run First

When an event triggers a transition, the evaluation lifecycle is strict: Guards run first, then Exit Actions, then Transition Actions (`assign`), then Entry Actions.

Because Guards evaluate before any transition actions execute, a Guard cannot validate a context update that happens on the exact same transition. It can only evaluate the *current* context.

## Try it: Interactive Lab: The Guarded Form

Use the top toolbar to interact with the Multi-Step Form machine. Try clicking 'Next Step' while the email is invalid to see the Guard block the transition and gamify the error state!

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

## Check yourself

### Question 1

In XState v5, if an event triggers a transition that has both a guard and an `assign()` action, which evaluates first?

- A. The `assign()` action executes first to update the context, then the guard checks the new context.
- B. The guard evaluates first against the current context. If true, the transition proceeds and `assign()` executes.
- C. They run simultaneously in parallel.

Correct answer: B

Guards act as the bouncer at the door of the transition. They always evaluate the *current* state and context before any transition actions (like `assign`) are allowed to run.

- Why not A: False. This is a common misconception.
- Why not C: XState execution is strictly sequential and synchronous.

## Remember this

- Context stores arbitrary data alongside finite states to prevent state explosion.
- Actions (like `assign()`) execute fire-and-forget side effects during transitions.
- Guards are predicate functions that conditionally block or allow transitions.
- Guards always evaluate against the current context before any actions run.
