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

query-cancellation.md

165 lines3.2 KB

1---
2id: query-cancellation
3title: Query Cancellation
4ref: docs/framework/react/guides/query-cancellation.md
5replace:
6 {
7 '@tanstack/react-query': '@tanstack/solid-query',
8 'useMutationState[(]': 'useMutationState(() => ',
9 'useMutation[(]': 'useMutation(() => ',
10 'useQuery[(]': 'useQuery(() => ',
11 'useQueries[(]': 'useQueries(() => ',
12 'useInfiniteQuery[(]': 'useInfiniteQuery(() => ',
13 }
14---
15​
16[//]: # 'Example'
17​
18```tsx
19const todosQuery = useQuery(() => ({
20 queryKey: ['todos'],
21 queryFn: async ({ signal }) => {
22 const todosResponse = await fetch('/todos', {
23 // Pass the signal to one fetch
24 signal,
25 })
26 const todos = await todosResponse.json()
27​
28 const todoDetails = todos.map(async ({ details }) => {
29 const response = await fetch(details, {
30 // Or pass it to several
31 signal,
32 })
33 return response.json()
34 })
35​
36 return Promise.all(todoDetails)
37 },
38}))
39```
40​
41[//]: # 'Example'
42[//]: # 'Example2'
43​
44```tsx
45import axios from 'axios'
46​
47const todosQuery = useQuery(() => ({
48 queryKey: ['todos'],
49 queryFn: ({ signal }) =>
50 axios.get('/todos', {
51 // Pass the signal to `axios`
52 signal,
53 }),
54}))
55```
56​
57[//]: # 'Example2'
58[//]: # 'Example3'
59​
60```tsx
61import axios from 'axios'
62​
63const todosQuery = useQuery(() => ({
64 queryKey: ['todos'],
65 queryFn: ({ signal }) => {
66 // Create a new CancelToken source for this request
67 const CancelToken = axios.CancelToken
68 const source = CancelToken.source()
69​
70 const promise = axios.get('/todos', {
71 // Pass the source token to your request
72 cancelToken: source.token,
73 })
74​
75 // Cancel the request if TanStack Query signals to abort
76 signal?.addEventListener('abort', () => {
77 source.cancel('Query was cancelled by TanStack Query')
78 })
79​
80 return promise
81 },
82}))
83```
84​
85[//]: # 'Example3'
86[//]: # 'Example4'
87​
88```tsx
89const todosQuery = useQuery(() => ({
90 queryKey: ['todos'],
91 queryFn: ({ signal }) => {
92 return new Promise((resolve, reject) => {
93 var oReq = new XMLHttpRequest()
94 oReq.addEventListener('load', () => {
95 resolve(JSON.parse(oReq.responseText))
96 })
97 signal?.addEventListener('abort', () => {
98 oReq.abort()
99 reject()
100 })
101 oReq.open('GET', '/todos')
102 oReq.send()
103 })
104 },
105}))
106```
107​
108[//]: # 'Example4'
109[//]: # 'Example5'
110​
111```tsx
112const client = new GraphQLClient(endpoint)
113​
114const todosQuery = useQuery(() => ({
115 queryKey: ['todos'],
116 queryFn: ({ signal }) => {
117 client.request({ document: query, signal })
118 },
119}))
120```
121​
122[//]: # 'Example5'
123[//]: # 'Example6'
124​
125```tsx
126const todosQuery = useQuery(() => ({
127 queryKey: ['todos'],
128 queryFn: ({ signal }) => {
129 const client = new GraphQLClient(endpoint, {
130 signal,
131 })
132 return client.request(query, variables)
133 },
134}))
135```
136​
137[//]: # 'Example6'
138[//]: # 'Example7'
139​
140```tsx
141const todosQuery = useQuery(() => ({
142 queryKey: ['todos'],
143 queryFn: async ({ signal }) => {
144 const resp = await fetch('/todos', { signal })
145 return resp.json()
146 },
147}))
148​
149const queryClient = useQueryClient()
150​
151return (
152 <button
153 onClick={(e) => {
154 e.preventDefault()
155 queryClient.cancelQueries({ queryKey: ['todos'] })
156 }}
157 >
158 Cancel
159 </button>
161```
162​
163[//]: # 'Example7'
164[//]: # 'Limitations'
165[//]: # 'Limitations'