@xstate/svelte
TypeScript icon, indicating that this package has built-in type declarations

3.0.3 • Public • Published

@xstate/svelte

This package contains utilities for using XState with Svelte.

Quick Start

  1. 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

  1. 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>

Dependencies (0)

    Dev Dependencies (8)

    Package Sidebar

    Install

    npm i @xstate/svelte

    Weekly Downloads

    2,958

    Version

    3.0.3

    License

    MIT

    Unpacked Size

    8.06 kB

    Total Files

    14

    Last publish

    Collaborators

    • xstate-release-bot
    • andarist
    • davidkpiano