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

disabling-queries.md

95 lines2.1 KB

1---
2id: disabling-queries
3title: Disabling/Pausing Queries
4ref: docs/framework/react/guides/disabling-queries.md
5replace: { 'useQuery': 'injectQuery' }
6---
7​
8[//]: # 'Example'
9​
10```angular-ts
11@Component({
12 selector: 'todos',
13 template: `<div>
14 <button (click)="todosQuery.refetch()">Fetch Todos</button>
15​
16 @if (todosQuery.data()) {
17 <ul>
18 @for (todo of todosQuery.data(); track todo.id) {
19 <li>{{ todo.title }}</li>
20 }
21 </ul>
22 } @else {
23 @if (todosQuery.isError()) {
24 <span>Error: {{ todosQuery.error().message }}</span>
25 } @else if (todosQuery.isLoading()) {
26 <span>Loading...</span>
27 } @else if (!todosQuery.isLoading() && !todosQuery.isError()) {
28 <span>Not ready ...</span>
29 }
30 }
31​
32 <div>{{ todosQuery.isLoading() ? 'Fetching...' : '' }}</div>
33 </div>`,
34})
35export class TodosComponent {
36 readonly todosQuery = injectQuery(() => ({
37 queryKey: ['todos'],
38 queryFn: fetchTodoList,
39 enabled: false,
40 }))
42```
43​
44[//]: # 'Example'
45[//]: # 'Example2'
46​
47```angular-ts
48@Component({
49 selector: 'todos',
50 template: `
51 <div>
52 // πŸš€ applying the filter will enable and execute the query
53 <filters-form onApply="filter.set" />
54 <todos-table data="todosQuery.data()" />
55 </div>
56 `,
57})
58export class TodosComponent {
59 filter = signal('')
60​
61 readonly todosQuery = injectQuery(() => ({
62 queryKey: ['todos', this.filter()],
63 queryFn: () => fetchTodos(this.filter()),
64 enabled: !!this.filter(),
65 }))
67```
68​
69[//]: # 'Example2'
70[//]: # 'Example3'
71​
72```angular-ts
73import { skipToken, injectQuery } from '@tanstack/angular-query'
74​
75@Component({
76 selector: 'todos',
77 template: `
78 <div>
79 // πŸš€ applying the filter will enable and execute the query
80 <filters-form onApply="filter.set" />
81 <todos-table data="todosQuery.data()" />
82 </div>
83 `,
84})
85export class TodosComponent {
86 filter = signal('')
87​
88 readonly todosQuery = injectQuery(() => ({
89 queryKey: ['todos', this.filter()],
90 queryFn: this.filter() ? () => fetchTodos(this.filter()) : skipToken,
91 }))
93```
94​
95[//]: # 'Example3'