Installation
Installxstate and @xstate/vue:
Quick Start
The@xstate/vue package provides composition functions to use XState with Vue 3:
Available Hooks
The@xstate/vue package provides these composition functions:
useActor(logic, options?)- Creates and starts an actor from any XState logicuseActorRef(logic, options?, observerOrListener?)- Returns just the actor ref without reactive snapshotuseMachine(machine, options?)- Alias foruseActor, specifically typed for state machinesuseSelector(actorRef, selector, compare?)- Derives and subscribes to a selected value from an actor
Return Values
useActor / useMachine
Returns an object with:snapshot- A VueRefcontaining the current snapshotsend- Function to send events to the actoractorRef- The underlying actor reference
useSelector
Returns a VueRef containing the selected value:
Lifecycle
The actor is automatically started when the component is mounted and stopped when unmounted:onMounted- Actor is started and subscriptions are createdonBeforeUnmount- Actor is stopped and subscriptions are cleaned up
TypeScript
All hooks are fully typed when using TypeScript:Next Steps
useActor
Learn about the useActor composition function
useSelector
Select derived values from actor snapshots