Package Exports
- @xstate/react
- @xstate/react/dist/xstate-react.cjs.js
- @xstate/react/dist/xstate-react.esm.js
- @xstate/react/fsm
- @xstate/react/fsm/dist/xstate-react-fsm.cjs.js
- @xstate/react/fsm/dist/xstate-react-fsm.esm.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
- Install
xstateand@xstate/react:
npm i xstate @xstate/reactVia 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
- Import the
useMachinehook:
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>
);
};