useActorRef hook creates an actor from logic and returns a stable actor reference without subscribing to state changes. This is useful when you want to send events to an actor but don’t need to re-render when the state changes.
Type Signature
Parameters
logic- The actor logic (machine, promise, callback, etc.) to create an actor fromoptions(optional) - Actor options including:input- Input data for the actorsystemId- System ID for the actorsnapshot- Initial snapshot for rehydrationinspect- Inspector for debugging
observerOrListener(optional) - Observer object or listener function to subscribe to state changes without causing re-renders
Return Value
Returns an actor reference with methods:send(event)- Send an event to the actorgetSnapshot()- Get the current snapshotsubscribe(observer)- Subscribe to state changesstart()- Start the actor (called automatically)stop()- Stop the actor (called automatically on unmount)
Basic Usage
Send Events Without Re-rendering
Combine with useSelector
Advanced Usage
With Observer Callback
With Observer Object
Parent-Child Actor Communication
Imperative Event Sending
Getting Snapshot Manually
Rehydration and Persistence
Important Notes
- The actor is automatically started when the component mounts
- The actor is automatically stopped when the component unmounts
- The actor reference is stable across re-renders
- Use this hook when you don’t need the component to re-render on state changes
- Combine with
useSelectorfor optimized re-renders on specific state slices - The observer/listener parameter doesn’t cause re-renders
Performance Benefits
useActorRef is more performant than useActor when you don’t need to subscribe to all state changes:
See Also
- useActor - Create and subscribe to an actor
- useSelector - Subscribe to derived state
- createActorContext - Share actors via context