@jer-0/xstate-svelte4
v2.0.1
Published
XState tools for Svelte
Downloads
1
Maintainers
Readme
@xstate/svelte
This package contains utilities for using XState with Svelte.
Quick Start
- Install
xstate
and@xstate/svelte
:
npm i xstate @xstate/svelte
Via CDN
<script src="https://unpkg.com/@xstate/svelte/dist/xstate-svelte.min.js"></script>
By using the global variable XStateSvelte
or
<script src="https://unpkg.com/@xstate/svelte/dist/xstate-svelte.fsm.min.js"></script>
By using the global variable XStateSvelteFSM
- Import
useMachine
<script>
import { useMachine } from '@xstate/svelte';
import { createMachine } from 'xstate';
const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
states: {
inactive: {
on: { TOGGLE: 'active' }
},
active: {
on: { TOGGLE: 'inactive' }
}
}
});
const { state, send } = useMachine(toggleMachine);
</script>
<button on:click={() => send('TOGGLE')}>
{$state.value === 'inactive'
? 'Click to activate'
: 'Active! Click to deactivate'}
</button>