xstate

State machines and statecharts for reliable app logic — make impossible states impossible.

xstate

General JS

State machines and statecharts for reliable app logic — make impossible states impossible.

Fit

Bundle (gzip)

14.5 kB

45.9 kB raw

Install size includes transitive dependencies. Bundle size is gzipped browser payload.

Freshness

The Problem

Complex flows — checkout, multi-step forms, media players — quickly rot into a tangle of boolean flags (isLoading, isError, hasSubmitted) that can combine into contradictory states. Reasoning about "what can happen next" becomes guesswork, and edge cases slip through.

What It Does

XState lets you model logic as an explicit finite state machine or statechart: named states, guarded transitions, and side effects that only run in the states you allow. Because transitions are declared up front, illegal combinations simply can't occur, and the machine doubles as living documentation you can visualize.

Installation

bash
npm install xstate

Usage Example

js
import { createMachine, createActor } from 'xstate';

const toggleMachine = createMachine({
  id: 'toggle',
  initial: 'inactive',
  states: {
    inactive: { on: { TOGGLE: 'active' } },
    active: { on: { TOGGLE: 'inactive' } },
  },
});

const actor = createActor(toggleMachine).start();
actor.send({ type: 'TOGGLE' }); // -> 'active'

Related packages