Skip to main content
TanStack Store provides Svelte support through the @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

This package requires Svelte 5.0+ as it uses runes ($state and $effect).

Basic Usage

The primary way to use TanStack Store in Svelte is through the useStore function:

The useStore Function

The useStore function subscribes your component to store updates using Svelte 5’s runes.

Signature

Parameters

  • store - The store instance to subscribe to
  • selector - (Optional) A function that selects which part of the state you need. Defaults to returning the entire state
  • options.equal - (Optional) A custom equality function. Defaults to shallow

Return Value

Returns an object with a readonly current 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 a shallow 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 $state internally to track changes
  • Uses $effect for 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

  1. Use selective subscriptions: Subscribe only to the state you need
  2. Leverage runes: Combine with $derived for computed values
  3. Multiple stores: Create focused stores for different domains
  4. 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:
Why use TanStack Store over Svelte 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:

TypeScript Support

The Svelte adapter provides full TypeScript support: