Overview
TheuseSelector function subscribes to an actor and returns a derived value from its snapshot as a Svelte readable store. 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:
Performance Optimization
UseuseSelector to optimize rendering by only subscribing to specific parts of state:
Multiple Selectors
Create multiple selectors for different values:Reactive Statements
Combine selectors with Svelte’s reactive statements:Selecting from Spawned Actors
UseuseSelector with actors spawned from a parent machine:
Manual Store Subscription
SinceuseSelector returns a Svelte store, you can subscribe manually:
TypeScript
The selector function is fully typed:Implementation Details
TheuseSelector function:
- Gets the initial selected value from the actor’s current snapshot
- Creates a Svelte readable store with the selected value
- Subscribes to actor changes in the store’s start function
- Calls the selector on each new snapshot
- Compares the new selected value with the previous using the comparison function
- Only updates the store when the comparison returns
false - Automatically unsubscribes when the store has no subscribers
Related
useActor
Create and manage actors with reactive stores
Svelte Integration
Back to Svelte integration overview