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

disabling-queries.md

102 lines2.3 KB

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 )
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 )
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 )
100```
101​
102[//]: # 'Example3'