Open History to see the last commit
1---
2id: disabling-queries
3title: Disabling/Pausing Queries
4ref: docs/framework/react/guides/disabling-queries.md
5---
6β
7[//]: # 'Example'
8β
9```tsx
10import { Switch, Match, Show, For } from 'solid-js'
11β
12function Todos() {
13 const todosQuery = useQuery(() => ({
14 queryKey: ['todos'],
15 queryFn: fetchTodoList,
16 enabled: false,
17 }))
18β
19 return (
20 <div>
21 <button onClick={() => todosQuery.refetch()}>Fetch Todos</button>
22β
23 <Switch>
24 <Match when={todosQuery.data}>
25 <ul>
26 <For each={todosQuery.data}>{(todo) => <li>{todo.title}</li>}</For>
27 </ul>
28 </Match>
29 <Match when={todosQuery.isError}>
30 <span>Error: {todosQuery.error.message}</span>
31 </Match>
32 <Match when={todosQuery.isLoading}>
33 <span>Loading...</span>
34 </Match>
35 <Match when={true}>
36 <span>Not ready ...</span>
37 </Match>
38 </Switch>
39β
40 <div>{todosQuery.isFetching ? 'Fetching...' : null}</div>
41 </div>
42 )
43}
44```
45β
46[//]: # 'Example'
47[//]: # 'Example2'
48β
49```tsx
50import { createSignal } from 'solid-js'
51β
52function Todos() {
53 const [filter, setFilter] = createSignal('')
54β
55 const todosQuery = useQuery(() => ({
56 queryKey: ['todos', filter()],
57 queryFn: () => fetchTodos(filter()),
58 // β¬οΈ disabled as long as the filter is empty
59 enabled: !!filter(),
60 }))
61β
62 return (
63 <div>
64 {/* π applying the filter will enable and execute the query */}
65 <FiltersForm onApply={setFilter} />
66 <Show when={todosQuery.data}>
67 <TodosTable data={todosQuery.data} />
68 </Show>
69 </div>
70 )
71}
72```
73β
74[//]: # 'Example2'
75[//]: # 'Example3'
76β
77```tsx
78import { createSignal } from 'solid-js'
79import { skipToken, useQuery } from '@tanstack/solid-query'
80β
81function Todos() {
82 const [filter, setFilter] = createSignal<string | undefined>()
83β
84 const todosQuery = useQuery(() => ({
85 queryKey: ['todos', filter()],
86 // β¬οΈ disabled as long as the filter is undefined or empty
87 queryFn: filter() ? () => fetchTodos(filter()!) : skipToken,
88 }))
89β
90 return (
91 <div>
92 {/* π applying the filter will enable and execute the query */}
93 <FiltersForm onApply={setFilter} />
94 <Show when={todosQuery.data}>
95 <TodosTable data={todosQuery.data} />
96 </Show>
97 </div>
98 )
99}
100```
101β
102[//]: # 'Example3'