📦
State Management1.5M+/wkMIT

xstate

XState is a state management and orchestration library that uses finite state machines and statecharts to model complex application logic. Instead of managing s

Installation

npm
npm install xstate
yarn
yarn add xstate
pnpm
pnpm add xstate

Import

ESM
import { createMachine, interpret } from 'xstate';

Quick Example

usage
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'

About xstate

XState is a state management and orchestration library that uses finite state machines and statecharts to model complex application logic. Instead of managing state as arbitrary variables, XState defines explicit states (idle, loading, success, error), events that trigger transitions between states, and effects (actions, services) that execute during transitions. This approach makes impossible states impossible — if your machine is in the 'loading' state, it can only transition to 'success' or 'error', not back to 'loading'. XState provides createMachine for defining state machines, interpret/createActor for running them, and framework integrations (@xstate/react, @xstate/vue, @xstate/svelte) for using machines in UI components. Machines can spawn child actors for concurrent processes, use guards for conditional transitions, invoke promises and callbacks as services, and compose hierarchically through parallel and nested states. The XState Visualizer provides an interactive diagram of your machine's states and transitions, invaluable for understanding and documenting complex flows. XState is particularly effective for modeling multi-step forms, authentication flows, media players, complex UI interactions, and any scenario where the valid sequence of state transitions needs to be precisely defined and enforced.

Quick Facts

Packagexstate
CategoryState Management
Weekly Downloads1.5M+
LicenseMIT
Installnpm install xstate

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.