xstate
Finite State Machines and Statecharts for the Modern Web.
Last updated 2 years ago by xstate-release-bot .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install xstate 
SYNC missed versions from official npm registry.


XState
JavaScript state machines and statecharts

npm version Statecharts gitter chat

JavaScript and TypeScript finite state machines and statecharts for the modern web.

📖 Read the documentation 📑 Adheres to the SCXML specification.

Packages

Super quick start

npm install xstate
import { createMachine, interpret } from 'xstate';

// Stateless machine definition
// machine.transition(...) is a pure function used by the interpreter.
const toggleMachine = createMachine({
  id: 'toggle',
  initial: 'inactive',
  states: {
    inactive: { on: { TOGGLE: 'active' } },
    active: { on: { TOGGLE: 'inactive' } }
  }
});

// Machine instance with internal state
const toggleService = interpret(toggleMachine)
  .onTransition((state) => console.log(state.value))
  .start();
// => 'inactive'

toggleService.send({ type: 'TOGGLE' });
// => 'active'

toggleService.send({ type: 'TOGGLE' });
// => 'inactive'

Visualizer

Visualize, simulate, and share your statecharts in XState Viz!

xstate visualizer

Why?

Statecharts are a formalism for modeling stateful, reactive systems. This is useful for declaratively describing the behavior of your application, from the individual components to the overall application logic.

Read 📽 the slides (🎥 video) or check out these resources for learning about the importance of finite state machines and statecharts in user interfaces:

Finite State Machines

Light Machine
import { createMachine } from 'xstate';

const lightMachine = createMachine({
  id: 'light',
  initial: 'green',
  states: {
    green: {
      on: {
        TIMER: 'yellow'
      }
    },
    yellow: {
      on: {
        TIMER: 'red'
      }
    },
    red: {
      on: {
        TIMER: 'green'
      }
    }
  }
});

const currentState = 'green';

const nextState = lightMachine.transition(currentState, { type: 'TIMER' })
  .value;

// => 'yellow'

Hierarchical (Nested) State Machines

Hierarchical Light Machine
import { createMachine } 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 currentState = 'yellow';

const nextState = lightMachine.transition(currentState, { type: 'TIMER' })
  .value;
// => {
//   red: 'walk'
// }

lightMachine.transition('red.walk', { type: 'PED_TIMER' }).value;
// => {
//   red: 'wait'
// }

Object notation for hierarchical states:

// ...
const waitState = lightMachine.transition(
  { red: 'walk' },
  { type: 'PED_TIMER' }
).value;

// => { red: 'wait' }

lightMachine.transition(waitState, { type: 'PED_TIMER' }).value;

// => { red: 'stop' }

lightMachine.transition({ red: 'stop' }, { type: 'TIMER' }).value;

// => 'green'

Parallel State Machines

Parallel state machine
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 boldState = wordMachine.transition('bold.off', { type: 'TOGGLE_BOLD' })
  .value;

// {
//   bold: 'on',
//   italics: 'off',
//   underline: 'off',
//   list: 'none'
// }

const nextState = wordMachine.transition(
  {
    bold: 'off',
    italics: 'off',
    underline: 'on',
    list: 'bullets'
  },
  { type: 'TOGGLE_ITALICS' }
).value;

// {
//   bold: 'off',
//   italics: 'on',
//   underline: 'on',
//   list: 'bullets'
// }

History States

Machine with history state
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 checkState = paymentMachine.transition('method.cash', {
  type: 'SWITCH_CHECK'
});

// => State {
//   value: { method: 'check' },
//   history: State { ... }
// }

const reviewState = paymentMachine.transition(checkState, { type: 'NEXT' });

// => State {
//   value: 'review',
//   history: State { ... }
// }

const previousState = paymentMachine.transition(reviewState, {
  type: 'PREVIOUS'
}).value;

// => { method: 'check' }

Sponsors

Huge thanks to the following companies for sponsoring xstate. You can sponsor further xstate development on OpenCollective.

Current Tags

  • 5.0.0-alpha.6                                ...           alpha (2 years ago)
  • 5.0.0-beta.54                                ...           beta (a year ago)
  • 5.18.2                                ...           latest (2 months ago)

248 Versions

  • 5.18.2                                ...           2 months ago
  • 5.18.1                                ...           2 months ago
  • 5.18.0                                ...           3 months ago
  • 5.17.4                                ...           3 months ago
  • 5.17.3                                ...           3 months ago
  • 5.17.2                                ...           3 months ago
  • 5.17.1                                ...           3 months ago
  • 5.17.0                                ...           4 months ago
  • 5.16.0                                ...           4 months ago
  • 5.15.0                                ...           4 months ago
  • 5.14.0                                ...           5 months ago
  • 5.13.2                                ...           5 months ago
  • 5.13.1                                ...           5 months ago
  • 5.13.0                                ...           6 months ago
  • 5.12.0                                ...           7 months ago
  • 5.11.0                                ...           7 months ago
  • 5.10.0                                ...           7 months ago
  • 5.9.1                                ...           8 months ago
  • 5.9.0                                ...           8 months ago
  • 5.8.2                                ...           9 months ago
  • 5.8.1                                ...           9 months ago
  • 5.8.0                                ...           9 months ago
  • 5.7.1                                ...           9 months ago
  • 5.7.0                                ...           9 months ago
  • 5.6.2                                ...           9 months ago
  • 5.6.1                                ...           9 months ago
  • 5.6.0                                ...           10 months ago
  • 5.5.2                                ...           10 months ago
  • 5.5.1                                ...           10 months ago
  • 5.5.0                                ...           10 months ago
  • 5.4.1                                ...           a year ago
  • 5.4.0                                ...           a year ago
  • 5.3.1                                ...           a year ago
  • 5.3.0                                ...           a year ago
  • 5.2.1                                ...           a year ago
  • 5.2.0                                ...           a year ago
  • 5.1.0                                ...           a year ago
  • 5.0.2                                ...           a year ago
  • 5.0.1                                ...           a year ago
  • 5.0.0                                ...           a year ago
  • 5.0.0-beta.54                                ...           a year ago
  • 5.0.0-beta.53                                ...           a year ago
  • 5.0.0-beta.52                                ...           a year ago
  • 5.0.0-beta.51                                ...           a year ago
  • 5.0.0-beta.50                                ...           a year ago
  • 5.0.0-beta.49                                ...           a year ago
  • 5.0.0-beta.48                                ...           a year ago
  • 5.0.0-beta.47                                ...           a year ago
  • 5.0.0-beta.46                                ...           a year ago
  • 5.0.0-beta.45                                ...           a year ago
  • 5.0.0-beta.44                                ...           a year ago
  • 5.0.0-beta.43                                ...           a year ago
  • 5.0.0-beta.42                                ...           a year ago
  • 5.0.0-beta.41                                ...           a year ago
  • 5.0.0-beta.40                                ...           a year ago
  • 5.0.0-beta.39                                ...           a year ago
  • 5.0.0-beta.38                                ...           a year ago
  • 5.0.0-beta.37                                ...           a year ago
  • 4.38.3                                ...           a year ago
  • 5.0.0-beta.36                                ...           a year ago
  • 5.0.0-beta.35                                ...           a year ago
  • 5.0.0-beta.34                                ...           a year ago
  • 5.0.0-beta.33                                ...           a year ago
  • 5.0.0-beta.32                                ...           a year ago
  • 5.0.0-beta.31                                ...           a year ago
  • 5.0.0-beta.30                                ...           a year ago
  • 5.0.0-beta.29                                ...           a year ago
  • 5.0.0-beta.28                                ...           a year ago
  • 5.0.0-beta.27                                ...           a year ago
  • 5.0.0-beta.26                                ...           a year ago
  • 5.0.0-beta.25                                ...           a year ago
  • 5.0.0-beta.24                                ...           a year ago
  • 5.0.0-beta.23                                ...           a year ago
  • 5.0.0-beta.22                                ...           a year ago
  • 5.0.0-beta.21                                ...           a year ago
  • 5.0.0-beta.20                                ...           a year ago
  • 4.38.2                                ...           a year ago
  • 5.0.0-beta.19                                ...           a year ago
  • 4.38.1                                ...           a year ago
  • 5.0.0-beta.18                                ...           a year ago
  • 5.0.0-beta.17                                ...           a year ago
  • 5.0.0-beta.16                                ...           a year ago
  • 5.0.0-beta.15                                ...           a year ago
  • 4.38.0                                ...           a year ago
  • 5.0.0-beta.14                                ...           a year ago
  • 5.0.0-beta.13                                ...           a year ago
  • 5.0.0-beta.12                                ...           a year ago
  • 5.0.0-beta.11                                ...           a year ago
  • 5.0.0-beta.10                                ...           2 years ago
  • 5.0.0-beta.9                                ...           2 years ago
  • 4.37.2                                ...           2 years ago
  • 5.0.0-beta.8                                ...           2 years ago
  • 5.0.0-beta.7                                ...           2 years ago
  • 5.0.0-alpha.6                                ...           2 years ago
  • 5.0.0-alpha.5                                ...           2 years ago
  • 5.0.0-alpha.4                                ...           2 years ago
  • 5.0.0-alpha.3                                ...           2 years ago
  • 5.0.0-alpha.2                                ...           2 years ago
  • 4.37.1                                ...           2 years ago
  • 4.37.0                                ...           2 years ago
  • 4.36.0                                ...           2 years ago
  • 4.35.4                                ...           2 years ago
  • 4.35.3                                ...           2 years ago
  • 5.0.0-alpha.1                                ...           2 years ago
  • 4.35.2                                ...           2 years ago
  • 4.35.1                                ...           2 years ago
  • 4.35.0                                ...           2 years ago
  • 4.34.0                                ...           2 years ago
  • 4.33.6                                ...           2 years ago
  • 4.33.5                                ...           2 years ago
  • 4.33.4                                ...           2 years ago
  • 4.33.3                                ...           2 years ago
  • 4.33.2                                ...           2 years ago
  • 4.33.1                                ...           2 years ago
  • 4.33.0                                ...           2 years ago
  • 5.0.0-alpha.0                                ...           2 years ago
  • 4.32.1                                ...           3 years ago
  • 4.32.0                                ...           3 years ago
  • 4.31.0                                ...           3 years ago
  • 4.30.6                                ...           3 years ago
  • 4.30.5                                ...           3 years ago
  • 4.30.4                                ...           3 years ago
  • 4.30.3                                ...           3 years ago
  • 4.30.2                                ...           3 years ago
  • 4.30.1                                ...           3 years ago
  • 4.30.0                                ...           3 years ago
  • 4.29.0                                ...           3 years ago
  • 4.28.1                                ...           3 years ago
  • 4.28.0                                ...           3 years ago
  • 4.27.0                                ...           3 years ago
  • 4.26.1                                ...           3 years ago
  • 4.26.0                                ...           3 years ago
  • 4.27.0-pr2674-2021926101023                                ...           3 years ago
  • 4.26.0-pr2674-202191175724                                ...           3 years ago
  • 4.26.0-pr2674-20219612435                                ...           3 years ago
  • 4.26.0-pr2674-20219515814                                ...           3 years ago
  • 4.25.0                                ...           3 years ago
  • 4.24.1                                ...           3 years ago
  • 4.24.0                                ...           3 years ago
  • 4.23.4                                ...           3 years ago
  • 4.23.3                                ...           3 years ago
  • 4.23.2                                ...           3 years ago
  • 4.23.1                                ...           3 years ago
  • 4.23.0                                ...           3 years ago
  • 4.22.0                                ...           3 years ago
  • 4.20.2                                ...           3 years ago
  • 4.20.1                                ...           3 years ago
  • 4.20.0                                ...           3 years ago
  • 4.19.2                                ...           3 years ago
  • 4.19.1                                ...           4 years ago
  • 4.19.0                                ...           4 years ago
  • 4.18.0                                ...           4 years ago
  • 4.17.1                                ...           4 years ago
  • 4.17.0                                ...           4 years ago
  • 4.16.2                                ...           4 years ago
  • 4.16.1                                ...           4 years ago
  • 4.16.0                                ...           4 years ago
  • 4.15.4                                ...           4 years ago
  • 4.15.3                                ...           4 years ago
  • 4.15.2                                ...           4 years ago
  • 4.15.1                                ...           4 years ago
  • 4.15.0                                ...           4 years ago
  • 4.14.1                                ...           4 years ago
  • 4.14.0                                ...           4 years ago
  • 4.13.0                                ...           4 years ago
  • 4.12.0                                ...           4 years ago
  • 4.11.0                                ...           4 years ago
  • 4.10.0                                ...           4 years ago
  • 4.9.1                                ...           5 years ago
  • 4.9.0                                ...           5 years ago
  • 4.8.0                                ...           5 years ago
  • 4.7.8                                ...           5 years ago
  • 4.7.7                                ...           5 years ago
  • 4.7.6                                ...           5 years ago
  • 4.7.5                                ...           5 years ago
  • 4.7.3                                ...           5 years ago
  • 4.7.2                                ...           5 years ago
  • 4.7.1                                ...           5 years ago
  • 4.7.0                                ...           5 years ago
  • 4.7.0-rc7                                ...           5 years ago
  • 4.7.0-rc6                                ...           5 years ago
  • 4.7.0-rc5                                ...           5 years ago
  • 4.7.0-rc4                                ...           5 years ago
  • 4.7.0-rc3                                ...           5 years ago
  • 4.7.0-rc2                                ...           5 years ago
  • 4.7.0-rc1                                ...           5 years ago
  • 4.6.7                                ...           5 years ago
  • 4.6.6                                ...           5 years ago
  • 4.6.5                                ...           5 years ago
  • 4.6.4                                ...           5 years ago
  • 4.6.3                                ...           5 years ago
  • 4.6.2                                ...           5 years ago
  • 4.6.1                                ...           5 years ago
  • 4.6.0                                ...           5 years ago
  • 4.6.0-rc4                                ...           5 years ago
  • 4.6.0-rc3                                ...           5 years ago
  • 4.6.0-rc2                                ...           5 years ago
  • 4.6.0-rc1                                ...           5 years ago
  • 4.5.0                                ...           6 years ago
  • 4.4.0                                ...           6 years ago
  • 4.3.3                                ...           6 years ago
  • 4.3.2                                ...           6 years ago
  • 4.3.1                                ...           6 years ago
  • 4.3.0                                ...           6 years ago
  • 4.2.4                                ...           6 years ago
  • 4.2.3                                ...           6 years ago
  • 4.2.2                                ...           6 years ago
  • 4.2.1                                ...           6 years ago
  • 4.2.0                                ...           6 years ago
  • 4.1.2                                ...           6 years ago
  • 4.1.1                                ...           6 years ago
  • 4.1.0                                ...           6 years ago
  • 4.0.1                                ...           6 years ago
  • 4.0.0                                ...           6 years ago
  • 4.0.0-15                                ...           6 years ago
  • 4.0.0-14                                ...           6 years ago
  • 4.0.0-13                                ...           6 years ago
  • 4.0.0-12                                ...           6 years ago
  • 4.0.0-11                                ...           6 years ago
  • 4.0.0-10                                ...           6 years ago
  • 4.0.0-9                                ...           6 years ago
  • 4.0.0-8                                ...           6 years ago
  • 4.0.0-7                                ...           6 years ago
  • 4.0.0-6                                ...           6 years ago
  • 4.0.0-1                                ...           6 years ago
  • 4.0.0-0                                ...           6 years ago
  • 3.3.3                                ...           6 years ago
  • 3.3.2                                ...           6 years ago
  • 3.3.1                                ...           6 years ago
  • 3.3.0                                ...           6 years ago
  • 3.2.1                                ...           7 years ago
  • 3.2.0                                ...           7 years ago
  • 3.1.1                                ...           7 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.4                                ...           7 years ago
  • 3.0.3                                ...           7 years ago
  • 3.0.2                                ...           7 years ago
  • 3.0.1                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.2                                ...           7 years ago
  • 2.0.1                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 1.2.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Downloads
Total 3
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (0)
None
Dev Dependencies (6)

© 2010 - cnpmjs.org x YWFE | Home | YWFE