Open History to see the last commit
1---
2id: quick-start
3title: Quick Start
4---
5β
6The `@tanstack/svelte-query` package offers a 1st-class API for using TanStack Query via Svelte.
7β
8## Example
9β
10```svelte
11<script lang="ts">
12 import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query'
13 import Example from './lib/Example.svelte'
14β
15 const queryClient = new QueryClient()
16</script>
17β
18<QueryClientProvider client={queryClient}>
19 <Example />
20</QueryClientProvider>
21```
22β
23Then call any function (e.g. createQuery) from any component:
24β
25```svelte
26<script lang="ts">
27 import { createQuery } from '@tanstack/svelte-query'
28β
29 const query = createQuery(() => ({
30 queryKey: ['todos'],
31 queryFn: () => fetchTodos(),
32 }))
33</script>
34β
35<div>
36 {#if query.isPending}
37 <p>Loading...</p>
38 {:else if query.isError}
39 <p>Error: {query.error.message}</p>
40 {:else if query.isSuccess}
41 {#each query.data as todo}
42 <p>{todo.title}</p>
43 {/each}
44 {/if}
45</div>
46```
47β
48## Important Differences between Svelte Query & React Query
49β
50Svelte Query offers an API similar to React Query, but there are some key differences to be mindful of.
51β
52- Arguments to `svelte-query` primitives (like `createQuery`, `createMutation`) are functions, so that they can be tracked in a reactive scope.
53β
54```ts
55// β react version
56useQuery({
57 queryKey: ['todos', todo],
58 queryFn: fetchTodos,
59})
60β
61// β
svelte version
62createQuery(() => ({
63 queryKey: ['todos', todo],
64 queryFn: fetchTodos,
65}))
66```
67β
68- Svelte Query primitives do not support destructuring. The return value from these functions is a proxy, and their properties are dynamically resolved.
69β
70```svelte
71<script lang="ts">
72 import { createQuery } from '@tanstack/svelte-query'
73β
74 const query = createQuery(() => ({
75 queryKey: ['repoData'],
76 queryFn: () =>
77 fetch('https://api.github.com/repos/TanStack/query').then((res) =>
78 res.json(),
79 ),
80 }))
81</script>
82β
83<!-- β react version -- supports destructuring outside reactive context
84 const { isPending, error, data } = useQuery({
85 queryKey: ['repoData'],
86 queryFn: () =>
87 fetch('https://api.github.com/repos/TanStack/query').then(
88 (res) => res.json(),
89 ),
90 }) -->
91β
92<!-- β
access query properties in svelte reactive context -->
93<div>
94 {#if query.isPending}
95 <p>Loading...</p>
96 {:else if query.isError}
97 <p>Error: {query.error.message}</p>
98 {:else if query.isSuccess}
99 <div>
100 <h1>{query.data.name}</h1>
101 <p>{query.data.description}</p>
102 <strong>π {query.data.subscribers_count}</strong>
103 <strong>β¨ {query.data.stargazers_count}</strong>
104 <strong>π΄ {query.data.forks_count}</strong>
105 </div>
106 {/if}
107</div>
108```
109β
110- Runes values can be passed in directly to function arguments. Svelte Query will update the query automatically.
111β
112```svelte
113<script lang="ts">
114 import { createQuery } from '@tanstack/svelte-query'
115β
116 let enabled = $state(false)
117 let todoCount = $state(0)
118β
119 // β
passing a rune directly is safe and observers update
120 // automatically when the value of a rune changes
121 const todosQuery = createQuery(() => ({
122 queryKey: ['todos'],
123 queryFn: () => fetchTodos(),
124 enabled: enabled,
125 }))
126β
127 const todoDetailsQuery = createQuery(() => ({
128 queryKey: ['todo', todoCount],
129 queryFn: fetchTodo,
130 enabled: todoCount > 0,
131 }))
132</script>
133β
134<div>
135 {#if todosQuery.isPending}
136 <p>Loading...</p>
137 {:else if todosQuery.isError}
138 <p>Error: {todosQuery.error.message}</p>
139 {:else if todosQuery.isSuccess}
140 {#each todosQuery.data as todo}
141 <button onclick={() => (todoCount = todo.id)}>{todo.title}</button>
142 {/each}
143 {/if}
144 <button onclick={() => (enabled = !enabled)}>Toggle enabled</button>
145</div>
146```
147β
148- Errors can be caught and reset using Svelte's native `<svelte:boundary>` component.
149 Set `throwOnError` option to `true` to make sure errors are thrown to the `<svelte:boundary>` component.
150β
151- Since property tracking is handled through Svelte's fine-grained reactivity, options like `notifyOnChangeProps` are not needed