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

queries.md

92 lines1.7 KB

1---
2id: queries
3title: Queries
4ref: docs/framework/react/guides/queries.md
5replace:
6 {
7 'React': 'Svelte',
8 'react-query': 'svelte-query',
9 'or custom hooks': '',
10 'the `useQuery` hook': '`createQuery`',
11 'useQuery': 'createQuery',
12 }
13---
14​
15[//]: # 'Example'
16​
17```svelte
18<script lang="ts">
19 import { createQuery } from '@tanstack/svelte-query'
20 const todosQuery = createQuery(() => ({
21 queryKey: ['todos'],
22 queryFn: fetchTodoList,
23 }))
24</script>
25```
26​
27[//]: # 'Example'
28[//]: # 'Example2'
29​
30```ts
31const todosQuery = createQuery(() => ({
32 queryKey: ['todos'],
33 queryFn: fetchTodoList,
34}))
35```
36​
37[//]: # 'Example2'
38[//]: # 'Example3'
39​
40```svelte
41<script lang="ts">
42 import { createQuery } from '@tanstack/svelte-query'
43​
44 const todosQuery = createQuery(() => ({
45 queryKey: ['todos'],
46 queryFn: () => fetchTodos(),
47 }))
48</script>
49​
50<div>
51 {#if todosQuery.isPending}
52 <p>Loading...</p>
53 {:else if todosQuery.isError}
54 <p>Error: {todosQuery.error.message}</p>
55 {:else if todosQuery.isSuccess}
56 {#each todosQuery.data as todo}
57 <p>{todo.title}</p>
58 {/each}
59 {/if}
60</div>
61```
62​
63[//]: # 'Example3'
64​
65If booleans aren't your thing, you can always use the `status` state as well:
66​
67[//]: # 'Example4'
68​
69```svelte
70<script lang="ts">
71 const todosQuery = createQuery(() => ({
72 queryKey: ['todos'],
73 queryFn: () => fetchTodos(),
74 }))
75</script>
76​
77<div>
78 {#if todosQuery.status === 'pending'}
79 <p>Loading...</p>
80 {:else if todosQuery.status === 'error'}
81 <p>Error: {todosQuery.error.message}</p>
82 {:else if todosQuery.status === 'success'}
83 {#each todosQuery.data as todo}
84 <p>{todo.title}</p>
85 {/each}
86 {/if}
87</div>
88```
89​
90[//]: # 'Example4'
91[//]: # 'Materials'
92[//]: # 'Materials'