Overview
TheuseActor function creates an actor from XState logic and manages its lifecycle within a Svelte component.
Basic Usage
With Options
Pass options to configure the actor:Accessing the Actor Ref
TheactorRef allows direct interaction with the underlying actor:
Reactive Store
Thesnapshot is a Svelte readable store that automatically updates:
useMachine Alias
TheuseMachine function is an alias for useActor with the same API:
Lifecycle Management
The actor lifecycle is automatically managed:-
Immediate Start
- Actor is created and started immediately:
createActor(logic, options).start()
- Actor is created and started immediately:
-
Component Destroy (
onDestroy)- 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 Svelte’s context API:TypeScript
The function is fully typed:Implementation Details
TheuseActor function:
- Creates an actor using
useActorRef(logic, options) - Gets the initial snapshot
- Creates a Svelte readable store
- Subscribes to actor changes in the store’s start function
- Updates the store only when snapshot reference changes
- Automatically unsubscribes when the store has no subscribers
Related
useActorRef
Get actor ref without reactive store
useSelector
Select derived values from snapshots