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

default-import.input.tsx

210 lines11.9 KB

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(['foo', 'bar'])
12 useIsFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
13 useIsFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { context: undefined })
14 useIsFetching(['foo', 'bar'], { type: 'all', exact: true })
15 useIsFetching(['foo', 'bar'], { type: 'all', exact: true }, { context: undefined })
16 useIsFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
17 useIsFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { context: undefined })
18​
19 useIsMutating(['foo', 'bar'])
20 useIsMutating(['foo', 'bar'], { exact: true })
21 useIsMutating(['foo', 'bar'], { exact: true }, { context: undefined })
22 useIsMutating({ mutationKey: ['foo', 'bar'], exact: true })
23 useIsMutating({ mutationKey: ['foo', 'bar'], exact: true }, { context: undefined })
24​
25 // QueryClient methods
26 // --- Instantiated hook call.
27 const queryClient = useQueryClient()
28 queryClient.cancelQueries(['foo', 'bar'])
29 queryClient.cancelQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
30 queryClient.cancelQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { silent: true })
31 queryClient.cancelQueries(['foo', 'bar'], { type: 'all', exact: true })
32 queryClient.cancelQueries(['foo', 'bar'], { type: 'all', exact: true }, { silent: true })
33 queryClient.cancelQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
34 queryClient.cancelQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { silent: true })
35​
36 queryClient.getQueriesData(['foo', 'bar'])
37 queryClient.getQueriesData({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
38​
39 queryClient.invalidateQueries(['foo', 'bar'])
40 queryClient.invalidateQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
41 queryClient.invalidateQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
42 queryClient.invalidateQueries(['foo', 'bar'], { type: 'all', exact: true })
43 queryClient.invalidateQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
44 queryClient.invalidateQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
45 queryClient.invalidateQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
46​
47 queryClient.isFetching(['foo', 'bar'])
48 queryClient.isFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
49 queryClient.isFetching(['foo', 'bar'], { type: 'all', exact: true })
50 queryClient.isFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
51​
52 queryClient.refetchQueries(['foo', 'bar'])
53 queryClient.refetchQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
54 queryClient.refetchQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
55 queryClient.refetchQueries(['foo', 'bar'], { type: 'all', exact: true })
56 queryClient.refetchQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
57 queryClient.refetchQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
58 queryClient.refetchQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
59​
60 queryClient.removeQueries(['foo', 'bar'])
61 queryClient.removeQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
62 queryClient.removeQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
63​
64 queryClient.resetQueries(['foo', 'bar'])
65 queryClient.resetQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
66 queryClient.resetQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
67 queryClient.resetQueries(['foo', 'bar'], { type: 'all', exact: true })
68 queryClient.resetQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
69 queryClient.resetQueries({ queryKey: ['foo', 'bar'], exact: true })
70 queryClient.resetQueries({ queryKey: ['foo', 'bar'], exact: true }, { cancelRefetch: false, throwOnError: true })
71​
72 queryClient.setQueriesData(['foo', 'bar'], null)
73 queryClient.setQueriesData(['foo', 'bar'], null, { updatedAt: 1000 })
74 queryClient.setQueriesData({ queryKey: ['foo', 'bar'] }, null)
75 queryClient.setQueriesData({ queryKey: ['foo', 'bar'] }, null, { updatedAt: 1000 })
76​
77 queryClient.fetchQuery(['foo', 'bar'])
78 queryClient.fetchQuery(['foo', 'bar'], { queryKey: ['todos'], staleTime: 1000 })
79 queryClient.fetchQuery(['foo', 'bar'], { queryKey: ['todos'], queryFn: () => 'data', staleTime: 1000 })
80 queryClient.fetchQuery(['foo', 'bar'], () => 'data', { queryKey: ['todos'], staleTime: 1000 })
81 queryClient.fetchQuery(['foo', 'bar'], function myFn() { return 'data' }, { queryKey: ['todos'], staleTime: 1000 })
82 queryClient.fetchQuery({ queryKey: ['foo', 'bar'], queryFn: () => 'data', retry: true })
83​
84 const queryCache = queryClient.getQueryCache()
85​
86 queryCache.find(['foo', 'bar'])
87 queryCache.find(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
88 queryCache.find(['foo', 'bar'], { type: 'all', exact: true })
89​
90 queryCache.findAll(['foo', 'bar'])
91 queryCache.findAll(['foo', 'bar'], { type: 'all', exact: true })
92 queryCache.findAll(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
93 queryCache.findAll({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
94​
95 return <div>Example Component</div>
97​
98const globalQueryKey = ['module', 'level']
99​
100export const WithIdentifiers = () => {
101 const queryKey = ['foo', 'bar']
102 const mutationKey = ['posts', 'articles']
103 const filters = { type: 'all', exact: true } as const
104 const options = { context: undefined } as const
105 const mutationOptions = { exact: true, fetching: false } as const
106 const cancelOptions = { silent: true } as const
107 const invalidateOptions = { cancelRefetch: true, throwOnError: true } as const
108 const refetchOptions = { cancelRefetch: false, throwOnError: true } as const
109 const resetOptions = { cancelRefetch: false, throwOnError: true } as const
110 const fetchOptions = { queryFn: () => 'data', retry: true } as const
111 const queryFn = () => 'data'
112​
113 useIsFetching(queryKey)
114 useIsFetching(queryKey, filters)
115 useIsFetching(queryKey, filters, options)
116 useIsFetching(queryKey, { type: 'all', exact: true })
117 useIsFetching(queryKey, { type: 'all', exact: true }, { context: undefined })
118 useIsFetching(queryKey, { queryKey: ['todos'], ...filters }, options)
119 useIsFetching({ queryKey: queryKey, ...filters })
120 useIsFetching({ queryKey: queryKey, ...filters }, { context: undefined })
121​
122 useIsMutating(mutationKey)
123 useIsMutating(mutationKey, { exact: true, status: 'idle' })
124 useIsMutating(mutationKey, { ...mutationOptions, exact: false })
125 useIsMutating({ mutationKey, ...mutationOptions })
126 useIsMutating({ mutationKey: ['foo', 'bar'], exact: true, status: 'idle' })
127​
128 // QueryClient methods
129 // --- Instantiated hook call.
130 const queryClient = useQueryClient()
131 queryClient.cancelQueries(queryKey)
132 queryClient.cancelQueries(queryKey, filters)
133 queryClient.cancelQueries(queryKey, filters, cancelOptions)
134 queryClient.cancelQueries(queryKey, { type: 'all', exact: true })
135 queryClient.cancelQueries(queryKey, { type: 'all', exact: true }, { revert: true })
136 queryClient.cancelQueries(queryKey, { queryKey: ['todos'], ...filters }, cancelOptions)
137 queryClient.cancelQueries({ queryKey: queryKey, type: 'all', exact: true })
138 queryClient.cancelQueries({ queryKey: ['foo', 'bar'], ...filters }, cancelOptions)
139​
140 queryClient.getQueriesData(globalQueryKey)
141 queryClient.getQueriesData({ queryKey: globalQueryKey, ...filters })
142 queryClient.getQueriesData({ queryKey: ['foo', 'bar'], type: 'all' })
143​
144 queryClient.invalidateQueries(queryKey)
145 queryClient.invalidateQueries(queryKey, filters)
146 queryClient.invalidateQueries(queryKey, filters, invalidateOptions)
147 queryClient.invalidateQueries(queryKey, { queryKey: ['todos'], stale: true, ...filters })
148 queryClient.invalidateQueries(queryKey, { queryKey: ['todos'], stale: true, ...filters }, invalidateOptions)
149 queryClient.invalidateQueries({ queryKey: globalQueryKey, ...filters, stale: true })
150 queryClient.invalidateQueries({ queryKey: globalQueryKey, ...filters, stale: true }, invalidateOptions)
151​
152 queryClient.isFetching(globalQueryKey)
153 queryClient.isFetching(globalQueryKey, filters)
154 queryClient.isFetching(globalQueryKey, { queryKey: ['todos'], type: 'all', exact: true })
155 queryClient.isFetching(globalQueryKey, { queryKey: ['todos'], ...filters })
156 queryClient.isFetching({ queryKey: globalQueryKey, ...filters, stale: true })
157 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
158 queryClient.isFetching(queryKeysFromAnotherModule)
159​
160 queryClient.refetchQueries(queryKey)
161 queryClient.refetchQueries(queryKey, filters)
162 queryClient.refetchQueries(queryKey, filters, refetchOptions)
163 queryClient.refetchQueries(queryKey, { queryKey: ['todos'], ...filters }, { ...refetchOptions, cancelRefetch: true })
164 queryClient.refetchQueries({ queryKey: queryKey, ...filters })
165 queryClient.refetchQueries({ queryKey: queryKey, ...filters }, { ...refetchOptions, cancelRefetch: true })
166 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
167 queryClient.refetchQueries(queryKeysFromAnotherModule)
168 queryClient.refetchQueries(queryKeysFromAnotherModule, filters)
169 queryClient.refetchQueries(queryKeysFromAnotherModule, filters, refetchOptions)
170​
171 queryClient.removeQueries(queryKey)
172 queryClient.removeQueries(queryKey, filters)
173 queryClient.removeQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true })
174 queryClient.removeQueries({ queryKey, ...filters, stale: true })
175 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
176 queryClient.removeQueries(queryKeysFromAnotherModule)
177 queryClient.removeQueries(queryKeysFromAnotherModule, filters)
178​
179 queryClient.resetQueries(queryKey)
180 queryClient.resetQueries(queryKey, filters)
181 queryClient.resetQueries(queryKey, filters, resetOptions)
182 queryClient.resetQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true })
183 queryClient.resetQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true }, resetOptions)
184 queryClient.resetQueries({ queryKey, ...filters, stale: true })
185 queryClient.resetQueries({ queryKey, ...filters, stale: true }, resetOptions)
186 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
187 queryClient.resetQueries(queryKeysFromAnotherModule)
188 queryClient.resetQueries(queryKeysFromAnotherModule, filters)
189 queryClient.resetQueries(queryKeysFromAnotherModule, filters, resetOptions)
190​
191 queryClient.fetchQuery(queryKey)
192 queryClient.fetchQuery(queryKey, fetchOptions)
193 queryClient.fetchQuery(queryKey, { networkMode: 'always', ...fetchOptions })
194 queryClient.fetchQuery(queryKey, queryFn, fetchOptions)
195 queryClient.fetchQuery(queryKey, () => 'data', { networkMode: 'always', ...fetchOptions })
196 // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
197 queryClient.fetchQuery(queryKeysFromAnotherModule)
198 queryClient.fetchQuery(queryKeysFromAnotherModule, fetchOptions)
199 queryClient.fetchQuery(queryKeysFromAnotherModule, queryFn, fetchOptions)
201​
202export const SecondArgumentIsAFunctionExample = () => {
203 useQuery(ordersCacheKeys.groupOrders(id), () => api.getPatientGroupOrders(id).then((r) => r.data))
204​
205 const rest = 'rest'
206 const of = 1
207 const functionArguments = { foo: 'bar' }
208​
209 useQuery(ordersCacheKeys.groupOrders(id), () => api.getPatientGroupOrders(id).then((r) => r.data), rest, of, functionArguments)