Open History to see the last commit
1import * as React from 'react'
2import {
3 useIsFetching,
4 useIsMutating,
5 useQuery,
6 useQueryClient,
7} from '@tanstack/react-query'
8import { queryKeysFromAnotherModule } from '../another/module'
9β
10export const WithKnownParameters = () => {
11 useIsFetching({
12 queryKey: ['foo', 'bar']
13 })
14 useIsFetching({
15 queryKey: ['foo', 'bar'],
16 type: 'all',
17 exact: true
18 })
19 useIsFetching({
20 queryKey: ['foo', 'bar'],
21 type: 'all',
22 exact: true
23 }, { context: undefined })
24 useIsFetching({
25 queryKey: ['foo', 'bar'],
26 type: 'all',
27 exact: true
28 })
29 useIsFetching({
30 queryKey: ['foo', 'bar'],
31 type: 'all',
32 exact: true
33 }, { context: undefined })
34 useIsFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
35 useIsFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { context: undefined })
36β
37 useIsMutating({
38 mutationKey: ['foo', 'bar']
39 })
40 useIsMutating({
41 mutationKey: ['foo', 'bar'],
42 exact: true
43 })
44 useIsMutating({
45 mutationKey: ['foo', 'bar'],
46 exact: true
47 }, { context: undefined })
48 useIsMutating({ mutationKey: ['foo', 'bar'], exact: true })
49 useIsMutating({ mutationKey: ['foo', 'bar'], exact: true }, { context: undefined })
50β
51 // QueryClient methods
52 // --- Instantiated hook call.
53 const queryClient = useQueryClient()
54 queryClient.cancelQueries({
55 queryKey: ['foo', 'bar']
56 })
57 queryClient.cancelQueries({
58 queryKey: ['foo', 'bar'],
59 type: 'all',
60 exact: true
61 })
62 queryClient.cancelQueries({
63 queryKey: ['foo', 'bar'],
64 type: 'all',
65 exact: true
66 }, { silent: true })
67 queryClient.cancelQueries({
68 queryKey: ['foo', 'bar'],
69 type: 'all',
70 exact: true
71 })
72 queryClient.cancelQueries({
73 queryKey: ['foo', 'bar'],
74 type: 'all',
75 exact: true
76 }, { silent: true })
77 queryClient.cancelQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
78 queryClient.cancelQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { silent: true })
79β
80 queryClient.getQueriesData({
81 queryKey: ['foo', 'bar']
82 })
83 queryClient.getQueriesData({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
84β
85 queryClient.invalidateQueries({
86 queryKey: ['foo', 'bar']
87 })
88 queryClient.invalidateQueries({
89 queryKey: ['foo', 'bar'],
90 type: 'all',
91 exact: true
92 })
93 queryClient.invalidateQueries({
94 queryKey: ['foo', 'bar'],
95 type: 'all',
96 exact: true
97 }, { cancelRefetch: false, throwOnError: true })
98 queryClient.invalidateQueries({
99 queryKey: ['foo', 'bar'],
100 type: 'all',
101 exact: true
102 })
103 queryClient.invalidateQueries({
104 queryKey: ['foo', 'bar'],
105 type: 'all',
106 exact: true
107 }, { cancelRefetch: false, throwOnError: true })
108 queryClient.invalidateQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
109 queryClient.invalidateQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
110β
111 queryClient.isFetching({
112 queryKey: ['foo', 'bar']
113 })
114 queryClient.isFetching({
115 queryKey: ['foo', 'bar'],
116 type: 'all',
117 exact: true
118 })
119 queryClient.isFetching({
120 queryKey: ['foo', 'bar'],
121 type: 'all',
122 exact: true
123 })
124 queryClient.isFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
125β
126 queryClient.refetchQueries({
127 queryKey: ['foo', 'bar']
128 })
129 queryClient.refetchQueries({
130 queryKey: ['foo', 'bar'],
131 type: 'all',
132 exact: true
133 })
134 queryClient.refetchQueries({
135 queryKey: ['foo', 'bar'],
136 type: 'all',
137 exact: true
138 }, { cancelRefetch: false, throwOnError: true })
139 queryClient.refetchQueries({
140 queryKey: ['foo', 'bar'],
141 type: 'all',
142 exact: true
143 })
144 queryClient.refetchQueries({
145 queryKey: ['foo', 'bar'],
146 type: 'all',
147 exact: true
148 }, { cancelRefetch: false, throwOnError: true })
149 queryClient.refetchQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
150 queryClient.refetchQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
151β
152 queryClient.removeQueries({
153 queryKey: ['foo', 'bar']
154 })
155 queryClient.removeQueries({
156 queryKey: ['foo', 'bar'],
157 type: 'all',
158 exact: true
159 })
160 queryClient.removeQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
161β
162 queryClient.resetQueries({
163 queryKey: ['foo', 'bar']
164 })
165 queryClient.resetQueries({
166 queryKey: ['foo', 'bar'],
167 type: 'all',
168 exact: true
169 })
170 queryClient.resetQueries({
171 queryKey: ['foo', 'bar'],
172 type: 'all',
173 exact: true
174 }, { cancelRefetch: false, throwOnError: true })
175 queryClient.resetQueries({
176 queryKey: ['foo', 'bar'],
177 type: 'all',
178 exact: true
179 })
180 queryClient.resetQueries({
181 queryKey: ['foo', 'bar'],
182 type: 'all',
183 exact: true
184 }, { cancelRefetch: false, throwOnError: true })
185 queryClient.resetQueries({ queryKey: ['foo', 'bar'], exact: true })
186 queryClient.resetQueries({ queryKey: ['foo', 'bar'], exact: true }, { cancelRefetch: false, throwOnError: true })
187β
188 queryClient.setQueriesData(['foo', 'bar'], null)
189 queryClient.setQueriesData(['foo', 'bar'], null, { updatedAt: 1000 })
190 queryClient.setQueriesData({ queryKey: ['foo', 'bar'] }, null)
191 queryClient.setQueriesData({ queryKey: ['foo', 'bar'] }, null, { updatedAt: 1000 })
192β
193 queryClient.fetchQuery({
194 queryKey: ['foo', 'bar']
195 })
196 queryClient.fetchQuery({
197 queryKey: ['foo', 'bar'],
198 staleTime: 1000
199 })
200 queryClient.fetchQuery({
201 queryKey: ['foo', 'bar'],
202 queryFn: () => 'data',
203 staleTime: 1000
204 })
205 queryClient.fetchQuery({
206 queryKey: ['foo', 'bar'],
207 queryFn: () => 'data',
208 staleTime: 1000
209 })
210 queryClient.fetchQuery({
211 queryKey: ['foo', 'bar'],
212 queryFn: function myFn() { return 'data' },
213 staleTime: 1000
214 })
215 queryClient.fetchQuery({ queryKey: ['foo', 'bar'], queryFn: () => 'data', retry: true })
216β
217 const queryCache = queryClient.getQueryCache()
218β
219 queryCache.find({
220 queryKey: ['foo', 'bar']
221 })
222 queryCache.find({
223 queryKey: ['foo', 'bar'],
224 type: 'all',
225 exact: true
226 })
227 queryCache.find({
228 queryKey: ['foo', 'bar'],
229 type: 'all',
230 exact: true
231 })
232β
233 queryCache.findAll({
234 queryKey: ['foo', 'bar']
235 })
236 queryCache.findAll({
237 queryKey: ['foo', 'bar'],
238 type: 'all',
239 exact: true
240 })
241 queryCache.findAll({
242 queryKey: ['foo', 'bar'],
243 type: 'all',
244 exact: true
245 })
246 queryCache.findAll({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
247β
248 return <div>Example Component</div>
249}
250β
251const globalQueryKey = ['module', 'level']
252β
253export const WithIdentifiers = () => {
254 const queryKey = ['foo', 'bar']
255 const mutationKey = ['posts', 'articles']
256 const filters = { type: 'all', exact: true } as const
257 const options = { context: undefined } as const
258 const mutationOptions = { exact: true, fetching: false } as const
259 const cancelOptions = { silent: true } as const
260 const invalidateOptions = { cancelRefetch: true, throwOnError: true } as const
261 const refetchOptions = { cancelRefetch: false, throwOnError: true } as const
262 const resetOptions = { cancelRefetch: false, throwOnError: true } as const
263 const fetchOptions = { queryFn: () => 'data', retry: true } as const
264 const queryFn = () => 'data'
265β
266 useIsFetching({
267 queryKey: queryKey
268 })
269 useIsFetching({
270 queryKey: queryKey,
271 ...filters
272 })
273 useIsFetching({
274 queryKey: queryKey,
275 ...filters
276 }, options)
277 useIsFetching({
278 queryKey: queryKey,
279 type: 'all',
280 exact: true
281 })
282 useIsFetching({
283 queryKey: queryKey,
284 type: 'all',
285 exact: true
286 }, { context: undefined })
287 useIsFetching({
288 queryKey: queryKey,
289 ...filters
290 }, options)
291 useIsFetching({ queryKey: queryKey, ...filters })
292 useIsFetching({ queryKey: queryKey, ...filters }, { context: undefined })
293β
294 useIsMutating({
295 mutationKey: mutationKey
296 })
297 useIsMutating({
298 mutationKey: mutationKey,
299 exact: true,
300 status: 'idle'
301 })
302 useIsMutating({
303 mutationKey: mutationKey,
304 ...mutationOptions,
305 exact: false
306 })
307 useIsMutating({ mutationKey, ...mutationOptions })
308 useIsMutating({ mutationKey: ['foo', 'bar'], exact: true, status: 'idle' })
309β
310 // QueryClient methods
311 // --- Instantiated hook call.
312 const queryClient = useQueryClient()
313 queryClient.cancelQueries({
314 queryKey: queryKey
315 })
316 queryClient.cancelQueries({
317 queryKey: queryKey,
318 ...filters
319 })
320 queryClient.cancelQueries({
321 queryKey: queryKey,
322 ...filters
323 }, cancelOptions)
324 queryClient.cancelQueries({
325 queryKey: queryKey,
326 type: 'all',
327 exact: true
328 })
329 queryClient.cancelQueries({
330 queryKey: queryKey,
331 type: 'all',
332 exact: true
333 }, { revert: true })
334 queryClient.cancelQueries({
335 queryKey: queryKey,
336 ...filters
337 }, cancelOptions)
338 queryClient.cancelQueries({ queryKey: queryKey, type: 'all', exact: true })
339 queryClient.cancelQueries({ queryKey: ['foo', 'bar'], ...filters }, cancelOptions)
340β
341 queryClient.getQueriesData({
342 queryKey: globalQueryKey
343 })
344 queryClient.getQueriesData({ queryKey: globalQueryKey, ...filters })
345 queryClient.getQueriesData({ queryKey: ['foo', 'bar'], type: 'all' })
346β
347 queryClient.invalidateQueries({
348 queryKey: queryKey
349 })
350 queryClient.invalidateQueries({
351 queryKey: queryKey,
352 ...filters
353 })
354 queryClient.invalidateQueries({
355 queryKey: queryKey,
356 ...filters
357 }, invalidateOptions)
358 queryClient.invalidateQueries({
359 queryKey: queryKey,
360 stale: true,
361 ...filters
362 })
363 queryClient.invalidateQueries({
364 queryKey: queryKey,
365 stale: true,
366 ...filters
367 }, invalidateOptions)
368 queryClient.invalidateQueries({ queryKey: globalQueryKey, ...filters, stale: true })
369 queryClient.invalidateQueries({ queryKey: globalQueryKey, ...filters, stale: true }, invalidateOptions)
370β
371 queryClient.isFetching({
372 queryKey: globalQueryKey
373 })
374 queryClient.isFetching({
375 queryKey: globalQueryKey,
376 ...filters
377 })
378 queryClient.isFetching({
379 queryKey: globalQueryKey,
380 type: 'all',
381 exact: true
382 })
383 queryClient.isFetching({
384 queryKey: globalQueryKey,
385 ...filters
386 })
387 queryClient.isFetching({ queryKey: globalQueryKey, ...filters, stale: true })
388 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
389 queryClient.isFetching(queryKeysFromAnotherModule)
390β
391 queryClient.refetchQueries({
392 queryKey: queryKey
393 })
394 queryClient.refetchQueries({
395 queryKey: queryKey,
396 ...filters
397 })
398 queryClient.refetchQueries({
399 queryKey: queryKey,
400 ...filters
401 }, refetchOptions)
402 queryClient.refetchQueries({
403 queryKey: queryKey,
404 ...filters
405 }, { ...refetchOptions, cancelRefetch: true })
406 queryClient.refetchQueries({ queryKey: queryKey, ...filters })
407 queryClient.refetchQueries({ queryKey: queryKey, ...filters }, { ...refetchOptions, cancelRefetch: true })
408 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
409 queryClient.refetchQueries(queryKeysFromAnotherModule)
410 queryClient.refetchQueries(queryKeysFromAnotherModule, filters)
411 queryClient.refetchQueries(queryKeysFromAnotherModule, filters, refetchOptions)
412β
413 queryClient.removeQueries({
414 queryKey: queryKey
415 })
416 queryClient.removeQueries({
417 queryKey: queryKey,
418 ...filters
419 })
420 queryClient.removeQueries({
421 queryKey: queryKey,
422 ...filters,
423 stale: true
424 })
425 queryClient.removeQueries({ queryKey, ...filters, stale: true })
426 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
427 queryClient.removeQueries(queryKeysFromAnotherModule)
428 queryClient.removeQueries(queryKeysFromAnotherModule, filters)
429β
430 queryClient.resetQueries({
431 queryKey: queryKey
432 })
433 queryClient.resetQueries({
434 queryKey: queryKey,
435 ...filters
436 })
437 queryClient.resetQueries({
438 queryKey: queryKey,
439 ...filters
440 }, resetOptions)
441 queryClient.resetQueries({
442 queryKey: queryKey,
443 ...filters,
444 stale: true
445 })
446 queryClient.resetQueries({
447 queryKey: queryKey,
448 ...filters,
449 stale: true
450 }, resetOptions)
451 queryClient.resetQueries({ queryKey, ...filters, stale: true })
452 queryClient.resetQueries({ queryKey, ...filters, stale: true }, resetOptions)
453 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
454 queryClient.resetQueries(queryKeysFromAnotherModule)
455 queryClient.resetQueries(queryKeysFromAnotherModule, filters)
456 queryClient.resetQueries(queryKeysFromAnotherModule, filters, resetOptions)
457β
458 queryClient.fetchQuery({
459 queryKey: queryKey
460 })
461 queryClient.fetchQuery({
462 queryKey: queryKey,
463 ...fetchOptions
464 })
465 queryClient.fetchQuery({
466 queryKey: queryKey,
467 networkMode: 'always',
468 ...fetchOptions
469 })
470 queryClient.fetchQuery({
471 queryKey: queryKey,
472 queryFn: queryFn,
473 ...fetchOptions
474 })
475 queryClient.fetchQuery({
476 queryKey: queryKey,
477 queryFn: () => 'data',
478 networkMode: 'always',
479 ...fetchOptions
480 })
481 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
482 queryClient.fetchQuery(queryKeysFromAnotherModule)
483 queryClient.fetchQuery(queryKeysFromAnotherModule, fetchOptions)
484 queryClient.fetchQuery(queryKeysFromAnotherModule, queryFn, fetchOptions)
485}
486β
487export const SecondArgumentIsAFunctionExample = () => {
488 useQuery({
489 queryKey: ordersCacheKeys.groupOrders(id),
490 queryFn: () => api.getPatientGroupOrders(id).then((r) => r.data)
491 })
492β
493 const rest = 'rest'
494 const of = 1
495 const functionArguments = { foo: 'bar' }
496β
497 useQuery({
498 queryKey: ordersCacheKeys.groupOrders(id),
499 queryFn: () => api.getPatientGroupOrders(id).then((r) => r.data)
500 }, rest, of, functionArguments)
501}