This package contains utilities for using XState with React.
Note: This repository is a patched fork of the upstream
@xstate/reactpackage from hash 5dd9e25, maintained specifically to preserve support for the removed@xstate/fsmdependency.
- Install
xstateand@xstate/react:
npm i xstate@4.37.2 xstate-react-patched@3.2.2-patch.0- 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>
);
};