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

3.1.2 • Public • Published

@xstate/vue

This package contains utilities for using XState with Vue.

⚠️ Vue 2 Notice:

If you're using Vue 2.x, please see the Vue recipe instead, or use the xstate-vue2 package if you want to use the Vue Composition API.

Quick start

  1. Install xstate and @xstate/vue:
npm i xstate @xstate/vue

Via CDN

<script src="https://unpkg.com/@xstate/vue/dist/xstate-vue.min.js"></script>

By using the global variable XStateVue

  1. Import the useMachine composition function:
<script setup>
import { useMachine } from '@xstate/vue';
import { createMachine } from 'xstate';

const toggleMachine = createMachine({
  id: 'toggle',
  initial: 'inactive',
  states: {
    inactive: {
      on: { TOGGLE: 'active' }
    },
    active: {
      on: { TOGGLE: 'inactive' }
    }
  }
});

const { snapshot, send } = useMachine(toggleMachine);
</script>

<template>
  <button @click="send({ type: 'TOGGLE' })">
    {{
      snapshot.value === 'inactive'
        ? 'Click to activate'
        : 'Active! Click to deactivate'
    }}
  </button>
</template>

Dependencies (0)

    Dev Dependencies (5)

    Package Sidebar

    Install

    npm i @xstate/vue

    Weekly Downloads

    22,379

    Version

    3.1.2

    License

    MIT

    Unpacked Size

    14.6 kB

    Total Files

    18

    Last publish

    Collaborators

    • xstate-release-bot
    • andarist
    • davidkpiano