JSPM

  • Created
  • Published
  • Downloads 1771537
  • Score
    100M100P100Q190833F
  • License MIT

XState tools for React

Package Exports

  • @xstate/react
  • @xstate/react/es/fsm.js
  • @xstate/react/es/index.js
  • @xstate/react/fsm
  • @xstate/react/lib/fsm
  • @xstate/react/lib/fsm.js
  • @xstate/react/lib/index.js
  • @xstate/react/lib/useActor
  • @xstate/react/lib/useActor.js
  • @xstate/react/lib/useConstant
  • @xstate/react/lib/useConstant.js
  • @xstate/react/lib/useMachine
  • @xstate/react/lib/useMachine.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@xstate/react) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@xstate/react

This package contains utilities for using XState with React.

Quick start

  1. Install xstate and @xstate/react:
npm i xstate @xstate/react

Via CDN

<script src="https://unpkg.com/@xstate/react/dist/xstate-react.umd.min.js"></script>

By using the global variable XStateReact

or

<script src="https://unpkg.com/@xstate/react/dist/xstate-react-fsm.umd.min.js"></script>

By using the global variable XStateReactFSM

  1. Import the useMachine hook:
import { useMachine } from '@xstate/react';
import { createMachine } from 'xstate';

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

export const Toggler = () => {
  const [state, send] = useMachine(toggleMachine);

  return (
    <button onClick={() => send('TOGGLE')}>
      {state.value === 'inactive'
        ? 'Click to activate'
        : 'Active! Click to deactivate'}
    </button>
  );
};