Open History to see the last commit
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 }))
22}
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 )
62}
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 )
92}
93```
94β
95[//]: # 'Example4'
96[//]: # 'Materials'
97[//]: # 'Materials'