Installation
Installxstate and @xstate/solid:
Quick Start
The@xstate/solid package provides hooks to use XState with SolidJS:
Available Hooks
The@xstate/solid package provides these hooks:
useActor(logic, options?)- Creates and starts an actor from any XState logicuseActorRef(logic, options?)- Returns just the actor ref without reactive trackinguseMachine(machine, options?)- Alias foruseActor, specifically typed for state machinesfromActorRef(actorRef)- Subscribes to an existing actor and returns a tracked snapshot accessor
Return Values
useActor / useMachine
Returns a tuple of[snapshot, send, actorRef]:
snapshot- The current snapshot (tracked by SolidJS for granular reactivity)send- Function to send events to the actoractorRef- The underlying actor reference
fromActorRef
Returns an accessor function that returns the current snapshot:Granular Reactivity
SolidJS tracks which properties you access, so components only re-render when those specific values change:Lifecycle
The actor is automatically started when the component mounts and stopped when it unmounts:onMount- Actor is started viaactorRef.start()onCleanup- Actor is stopped viaactorRef.stop()
TypeScript
All hooks are fully typed when using TypeScript:Matching States
When using hierarchical and parallel machines, use SolidJS’sSwitch and Match components:
Persisted and Rehydrated State
You can persist and rehydrate state viaoptions.snapshot:
Next Steps
useActor
Learn about the useActor hook
fromActorRef
Subscribe to existing actors