# Repository: statelyai/xstate # Stars: 29477 ## CLAUDE.md AGENTS.md ## README.md
Actor-based state management & orchestration for complex app logic. β Documentation
| Template | |
|---|---|
| [π€ XState Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/main/templates/vanilla-ts) [](https://stackblitz.com/github/statelyai/xstate/tree/main/templates/vanilla-ts?file=%2Fsrc%2FfeedbackMachine.ts) | - XState v5 - TypeScript - _No framework_ |
| [βοΈ XState + React Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/main/templates/react-ts) [](https://stackblitz.com/github/statelyai/xstate/tree/main/templates/react-ts?file=%2Fsrc%2FfeedbackMachine.ts) | - [React](https://react.dev/) - XState v5 - TypeScript |
| [π XState + Vue Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/main/templates/vue-ts) [](https://stackblitz.com/github/statelyai/xstate/tree/main/templates/vue-ts?file=%2Fsrc%2FfeedbackMachine.ts) | - [Vue](https://vuejs.org/) - XState v5 - TypeScript |
| [π§‘ XState + Svelte Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/main/templates/svelte-ts) [](https://stackblitz.com/github/statelyai/xstate/tree/main/templates/svelte-ts?file=%2Fsrc%2FfeedbackMachine.ts) | - [Svelte](https://svelte.dev/) - XState v5 - TypeScript |
| Code | Statechart |
|---|---|
| ```js import { createMachine, createActor } from 'xstate'; const lightMachine = createMachine({ id: 'light', initial: 'green', states: { green: { on: { TIMER: 'yellow' } }, yellow: { on: { TIMER: 'red' } }, red: { on: { TIMER: 'green' } } } }); const actor = createActor(lightMachine); actor.subscribe((state) => { console.log(state.value); }); actor.start(); // logs 'green' actor.send({ type: 'TIMER' }); // logs 'yellow' ``` |
Open in Stately Studio |
| Code | Statechart |
|---|---|
| ```js import { createMachine, createActor } from 'xstate'; const pedestrianStates = { initial: 'walk', states: { walk: { on: { PED_TIMER: 'wait' } }, wait: { on: { PED_TIMER: 'stop' } }, stop: {} } }; const lightMachine = createMachine({ id: 'light', initial: 'green', states: { green: { on: { TIMER: 'yellow' } }, yellow: { on: { TIMER: 'red' } }, red: { on: { TIMER: 'green' }, ...pedestrianStates } } }); const actor = createActor(lightMachine); actor.subscribe((state) => { console.log(state.value); }); actor.start(); // logs 'green' actor.send({ type: 'TIMER' }); // logs 'yellow' actor.send({ type: 'TIMER' }); // logs { red: 'walk' } actor.send({ type: 'PED_TIMER' }); // logs { red: 'wait' } ``` |
Open in Stately Studio |
| Code | Statechart |
|---|---|
| ```ts import { createMachine, createActor } from 'xstate'; const wordMachine = createMachine({ id: 'word', type: 'parallel', states: { bold: { initial: 'off', states: { on: { on: { TOGGLE_BOLD: 'off' } }, off: { on: { TOGGLE_BOLD: 'on' } } } }, underline: { initial: 'off', states: { on: { on: { TOGGLE_UNDERLINE: 'off' } }, off: { on: { TOGGLE_UNDERLINE: 'on' } } } }, italics: { initial: 'off', states: { on: { on: { TOGGLE_ITALICS: 'off' } }, off: { on: { TOGGLE_ITALICS: 'on' } } } }, list: { initial: 'none', states: { none: { on: { BULLETS: 'bullets', NUMBERS: 'numbers' } }, bullets: { on: { NONE: 'none', NUMBERS: 'numbers' } }, numbers: { on: { BULLETS: 'bullets', NONE: 'none' } } } } } }); const actor = createActor(wordMachine); actor.subscribe((state) => { console.log(state.value); }); actor.start(); // logs { // bold: 'off', // italics: 'off', // underline: 'off', // list: 'none' // } actor.send({ type: 'TOGGLE_BOLD' }); // logs { // bold: 'on', // italics: 'off', // underline: 'off', // list: 'none' // } actor.send({ type: 'TOGGLE_ITALICS' }); // logs { // bold: 'on', // italics: 'on', // underline: 'off', // list: 'none' // } ``` |
Open in Stately Studio |
| Code | Statechart |
|---|---|
| ```js import { createMachine, createActor } from 'xstate'; const paymentMachine = createMachine({ id: 'payment', initial: 'method', states: { method: { initial: 'cash', states: { cash: { on: { SWITCH_CHECK: 'check' } }, check: { on: { SWITCH_CASH: 'cash' } }, hist: { type: 'history' } }, on: { NEXT: 'review' } }, review: { on: { PREVIOUS: 'method.hist' } } } }); const actor = createActor(paymentMachine); actor.subscribe((state) => { console.log(state.value); }); actor.start(); // logs { // value: { method: 'cash' }, // } actor.send({ type: 'SWITCH_CHECK' }); // logs { // value: { method: 'check' }, // } actor.send({ type: 'NEXT' }); // logs { // value: 'review', // } actor.send({ type: 'PREVIOUS' }); // logs { // value: { method: 'check' }, // } ``` |
Open in Stately Studio |