Prerequisites
Before starting, make sure you have:- Svelte 5.25.0 or higher installed
- A Svelte or SvelteKit project set up
@tanstack/svelte-queryinstalled
Your First Query
1
Set up the QueryClient
First, create a
QueryClient and wrap your app with QueryClientProvider in your root layout:+layout.svelte
2
Create your first query
Now create a component that fetches data using
createQuery:Posts.svelte
The
createQuery function takes an accessor function () => options that returns the query configuration. This allows the query to react to changes in dependencies.3
Understanding query states
Svelte Query provides several state properties to handle different scenarios:Key states:
isPending- Query has no data yet (initial load)isError- Query failedisSuccess- Query succeededisFetching- Query is fetching (includes background refetches)data- The actual query dataerror- The error object if query failed
Dynamic Queries
Queries can depend on reactive variables. The query automatically refetches when dependencies change:PostDetail.svelte
Mutations
UsecreateMutation to create, update, or delete data:
CreatePost.svelte
Mutation States
Mutations provide similar state properties:isPending- Mutation is in progressisError- Mutation failedisSuccess- Mutation succeededdata- The mutation result dataerror- The error object if mutation failedmutate()- Function to trigger the mutationmutateAsync()- Promise-based mutation function
Query Options
Customize query behavior with various options:All time values are in milliseconds. Use
staleTime to control when data is considered “stale” and needs refetching.Infinite Queries
For paginated or infinite scroll data, usecreateInfiniteQuery:
InfinitePosts.svelte
Infinite Query Properties
data.pages- Array of all fetched pagesdata.pageParams- Array of all page parametershasNextPage- Whether more pages are availablehasPreviousPage- Whether previous pages are availablefetchNextPage()- Load the next pagefetchPreviousPage()- Load the previous pageisFetchingNextPage- Next page is loadingisFetchingPreviousPage- Previous page is loading
Query Invalidation
Invalidate queries to force them to refetch:Using queryOptions Helper
For better type safety and reusability, use thequeryOptions helper:
queries.ts
Posts.svelte
Best Practices
1
Use meaningful query keys
Query keys should describe the data uniquely:
2
Handle loading and error states
Always provide feedback for pending and error states:
3
Configure staleTime appropriately
Set
staleTime based on how often your data changes:4
Invalidate queries after mutations
Keep your UI in sync by invalidating related queries:
Common Patterns
Dependent Queries
Execute a query only after another query succeeds:Optimistic Updates
Update UI immediately before server confirmation:Prefetching
Prefetch data before it’s needed:Next Steps
1
TypeScript Integration
Learn how to get full type safety with TypeScript.TypeScript Guide →
2
DevTools
Install and use the Svelte Query DevTools for debugging.DevTools Setup →
3
Advanced Guides
Explore advanced patterns like SSR, persisting, and more.Guides →