Skip to main content
TanStack Query for Svelte provides query and mutation functions for fetching, caching, and updating asynchronous data in Svelte applications. It includes full support for Svelte 5’s new runes.

Installation

TanStack Svelte Query requires Svelte 5.25.0 or later.

Setup

Wrap your application with QueryClientProvider:

Core Functions

createQuery

Fetch and cache data with createQuery:
Query results are returned as Svelte stores. Use the $ prefix to access reactive values in your template.

Reactive Query Keys

Svelte Query works seamlessly with Svelte’s reactive statements:

createMutation

Perform side effects with mutations:

createInfiniteQuery

Implement infinite scrolling:

Svelte 5 Runes Support

Using $state Rune

Svelte Query integrates seamlessly with Svelte 5’s runes:

Using $derived Rune

Using $effect Rune

Svelte Query’s internal implementation uses Svelte 5 runes ($state, $derived, $effect), providing optimal reactivity and performance.

Store API

Accessing Query State

Query results are Svelte stores with a special .value property:

Derived Stores

Create derived stores from query results:

Advanced Features

createQueries

Execute multiple queries in parallel:

Query Options Factory

useMutationState

Track all mutations globally:

useIsFetching

Show a global loading indicator:

HydrationBoundary

For SSR with SvelteKit:

TypeScript

Full TypeScript support:

Context API

Access the QueryClient in nested components:

Svelte-Specific Patterns

Reactive Queries with Stores

Component Props Reactivity

When using props in queries, always wrap the options in a function () => ({ ... }) to ensure reactivity.

Migration from Svelte 4

Svelte 5 brings significant changes to reactivity:

Performance Tips

Avoid Recreating Queries

Use Query Key Factories