@xstate/store-vue
v1.1.0
Published
XState Store for Vue
Downloads
21
Readme
@xstate/store-vue
Vue adapter for @xstate/store.
Installation
npm install @xstate/store-vueQuickstart
<script setup>
import { createStore, useSelector } from '@xstate/store-vue';
// ...
const store = createStore({
context: { count: 0 },
on: {
inc: (ctx) => ({ ...ctx, count: ctx.count + 1 })
}
});
const count = useSelector(store, (s) => s.context.count);
</script>
<template>
<button @click="store.send({ type: 'inc' })">Count: {{ count }}</button>
</template>API
useSelector(store, selector?, compare?)
A composable that subscribes to a store and returns a selected value as a readonly ref.
<script setup>
import { createStore, useSelector } from '@xstate/store-vue';
// ...
const store = createStore({
context: { count: 0 },
on: {
inc: (ctx) => ({ ...ctx, count: ctx.count + 1 })
}
});
const count = useSelector(store, (s) => s.context.count);
// or without selector (returns full snapshot)
const snapshot = useSelector(store);
</script>
<template>
<div>{{ count }}</div>
</template>Arguments:
store- Store created withcreateStore()selector?- Function to select a value from snapshotcompare?- Equality function (default:===)
Returns: Readonly ref of the selected value
Re-exports
All exports from @xstate/store are re-exported, including createStore, createStoreWithProducer, createAtom, and more.
See the XState Store docs for the full API, and the Vue-specific docs for more Vue examples.
