Overview
TheuseActor hook creates an actor from XState logic and manages its lifecycle within a SolidJS component.
Basic Usage
With Options
Pass options to configure the actor:Accessing the Actor Ref
The third element of the tuple is the actor ref:Granular Reactivity
SolidJS tracks property access for fine-grained reactivity:useMachine Alias
TheuseMachine hook is an alias for useActor with the same API:
Lifecycle Management
The actor lifecycle is automatically managed:-
Component Mount (
onMount)- Actor is started via
actorRef.start() - Cleanup registered via
onCleanup
- Actor is started via
-
Component Unmount
- Actor is stopped via
actorRef.stop()
- Actor is stopped via
Multiple Actors
You can create multiple actors in a single component:With Context
Share actors across components using SolidJS’s context API:Derived Values
Create derived values using SolidJS’screateMemo:
TypeScript
The hook is fully typed:Implementation Details
TheuseActor hook:
- Creates an actor using
useActorRef(logic, options) - Uses
fromActorRefto create a tracked accessor for the snapshot - Returns a tuple of
[snapshot, send, actorRef] - The snapshot is tracked by SolidJS’s reactivity system
- Component only re-renders when accessed properties change
Related
useActorRef
Get actor ref without reactive tracking
fromActorRef
Subscribe to existing actors