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

disabling-queries.md

56 lines1.2 KB

1---
2id: disabling-queries
3title: Disabling/Pausing Queries
4ref: docs/framework/react/guides/disabling-queries.md
5replace: { 'react-query': 'preact-query', 'React': 'Preact' }
6---
7​
8[//]: # 'Example2'
9​
10```tsx
11function Todos() {
12 const [filter, setFilter] = useState('')
13​
14 const { data } = useQuery({
15 queryKey: ['todos', filter],
16 queryFn: () => fetchTodos(filter),
17 // ⬇️ disabled as long as the filter is empty
18 enabled: !!filter,
19 })
20​
21 return (
22 <div>
23 // πŸš€ applying the filter will enable and execute the query
24 <FiltersForm onApply={setFilter} />
25 {data && <TodosTable data={data} />}
26 </div>
27 )
29```
30​
31[//]: # 'Example2'
32[//]: # 'Example3'
33​
34```tsx
35import { skipToken, useQuery } from '@tanstack/preact-query'
36​
37function Todos() {
38 const [filter, setFilter] = useState<string | undefined>()
39​
40 const { data } = useQuery({
41 queryKey: ['todos', filter],
42 // ⬇️ disabled as long as the filter is undefined or empty
43 queryFn: filter ? () => fetchTodos(filter) : skipToken,
44 })
45​
46 return (
47 <div>
48 // πŸš€ applying the filter will enable and execute the query
49 <FiltersForm onApply={setFilter} />
50 {data && <TodosTable data={data} />}
51 </div>
52 )
54```
55​
56[//]: # 'Example3'