Overview
TheuseSelector composition function subscribes to an actor and returns a derived value from its snapshot. It only triggers updates when the selected value changes.
Basic Usage
Custom Comparison
By default,useSelector uses strict equality (===) to compare values. Provide a custom comparison function for complex objects:
Reactive Actor Reference
You can pass a reactiveRef<Actor> to useSelector:
Performance Optimization
UseuseSelector to optimize rendering by only subscribing to specific parts of state:
Multiple Selectors
Create multiple selectors for different values:Selecting from Spawned Actors
UseuseSelector with actors spawned from a parent machine:
TypeScript
The selector function is fully typed:Implementation Details
TheuseSelector hook:
- Creates a shallow ref for the selected value
- Uses Vue’s
watchto monitor the actor ref - Subscribes to actor changes on mount
- Compares new selected values using the comparison function
- Only updates the ref when the comparison returns
false - Automatically unsubscribes on cleanup
Related
useActor
Create and manage actors with reactive snapshots
Vue Integration
Back to Vue integration overview