Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

quick-start.md

151 lines4.0 KB

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/tannerlinsley/react-query').then(
78 (res) => 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/tannerlinsley/react-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