@tanstack/svelte-store package, which integrates with Svelte 5’s runes-based reactivity system.
Installation
Install the Svelte adapter package:The
@tanstack/svelte-store package re-exports everything from @tanstack/store, so you only need to install the Svelte package.Svelte Version Requirements
Basic Usage
The primary way to use TanStack Store in Svelte is through theuseStore function:
The useStore Function
TheuseStore function subscribes your component to store updates using Svelte 5’s runes.
Signature
Parameters
store- The store instance to subscribe toselector- (Optional) A function that selects which part of the state you need. Defaults to returning the entire stateoptions.equal- (Optional) A custom equality function. Defaults toshallow
Return Value
Returns an object with a readonlycurrent property containing the selected state. Access it like count.current.
The return value uses
$state internally, so it’s reactive and will trigger re-renders when the selected value changes.Selector Optimization
The selector function allows you to subscribe to only the parts of state you need:Custom Equality Functions
For complex state selections, provide a custom equality function:Shallow Equality
The package exports ashallow utility for shallow object comparison (this is the default):
Complete Example: Todo App
Here’s a complete Todo application using Svelte 5:Svelte-Specific Considerations
Runes Integration
The Svelte adapter uses Svelte 5’s runes:- Uses
$stateinternally to track changes - Uses
$effectfor subscriptions and cleanup - Fully reactive and integrates with Svelte’s fine-grained reactivity
Working with Svelte’s Reactivity
You can combine store values with Svelte’s runes:Server-Side Rendering (SSR)
TanStack Store works with SvelteKit:Performance Tips
- Use selective subscriptions: Subscribe only to the state you need
- Leverage runes: Combine with
$derivedfor computed values - Multiple stores: Create focused stores for different domains
- Default shallow equality: Works well for most use cases
Derived Stores
Create derived stores that react to other stores:Comparison with Svelte Stores
You can use TanStack Store alongside Svelte’s built-in stores:- Type-safe selector functions
- Built-in shallow equality checking
- Consistent API across frameworks
- Advanced features like derived stores
API Reference
For detailed API documentation, see:- See the API reference for detailed documentation: