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

queries.md

97 lines1.9 KB

1---
2id: queries
3title: Queries
4ref: docs/framework/react/guides/queries.md
5---
6​
7[//]: # 'SubscribeDescription'
8​
9To subscribe to a query in your components, call the `useQuery` function with at least:
10[//]: # 'SubscribeDescription'
11​
12[//]: # 'Example'
13​
14```tsx
15import { useQuery } from '@tanstack/solid-query'
16​
17function App() {
18 const todosQuery = useQuery(() => ({
19 queryKey: ['todos'],
20 queryFn: fetchTodoList,
21 }))
23```
24​
25[//]: # 'Example'
26[//]: # 'Example2'
27​
28```tsx
29const todosQuery = useQuery(() => ({
30 queryKey: ['todos'],
31 queryFn: fetchTodoList,
32}))
33```
34​
35[//]: # 'Example2'
36[//]: # 'Example3'
37​
38```tsx
39import { Switch, Match, For } from 'solid-js'
40​
41function Todos() {
42 const todosQuery = useQuery(() => ({
43 queryKey: ['todos'],
44 queryFn: fetchTodoList,
45 }))
46​
47 return (
48 <Switch>
49 <Match when={todosQuery.isPending}>
50 <span>Loading...</span>
51 </Match>
52 <Match when={todosQuery.isError}>
53 <span>Error: {todosQuery.error.message}</span>
54 </Match>
55 <Match when={todosQuery.isSuccess}>
56 <ul>
57 <For each={todosQuery.data}>{(todo) => <li>{todo.title}</li>}</For>
58 </ul>
59 </Match>
60 </Switch>
61 )
63```
64​
65[//]: # 'Example3'
66[//]: # 'Example4'
67​
68```tsx
69import { Switch, Match, For } from 'solid-js'
70​
71function Todos() {
72 const todosQuery = useQuery(() => ({
73 queryKey: ['todos'],
74 queryFn: fetchTodoList,
75 }))
76​
77 return (
78 <Switch>
79 <Match when={todosQuery.status === 'pending'}>
80 <span>Loading...</span>
81 </Match>
82 <Match when={todosQuery.status === 'error'}>
83 <span>Error: {todosQuery.error.message}</span>
84 </Match>
85 <Match when={todosQuery.status === 'success'}>
86 <ul>
87 <For each={todosQuery.data}>{(todo) => <li>{todo.title}</li>}</For>
88 </ul>
89 </Match>
90 </Switch>
91 )
93```
94​
95[//]: # 'Example4'
96[//]: # 'Materials'
97[//]: # 'Materials'