Mohammad Asif cf937194cb Removed un-waned things 1. | 6 months ago | |
---|---|---|
.. | ||
dist | 6 months ago | |
es | 6 months ago | |
fsm | 6 months ago | |
lib | 6 months ago | |
LICENSE | 6 months ago | |
README.md | 6 months ago | |
package.json | 6 months ago |
This package contains utilities for using XState with React.
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
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>
);
};