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

named.input.tsx

289 lines7.7 KB

1import * as React from 'react'
2import axios from 'axios'
3import {
4 QueryClient as RenamedQueryClient,
5 useQueries as useRenamedUseQueries,
6 useQuery as useRenamedUseQuery,
7 useQueryClient as useRenamedUseQueryClient
8} from '@tanstack/react-query'
9​
10type Post = {
11 id: number
12 title: string
13 body: string
15​
16const queryFn = async (): Promise<Array<Post>> => {
17 const { data } = await axios.get(
18 'https://jsonplaceholder.typicode.com/posts',
19 )
20 return data
22​
23/**
24 * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform
25 * this usage.
26 */
27export const Example1 = () => {
28 const { data } = useRenamedUseQuery({
29 queryKey: ['posts'],
30 queryFn: queryFn,
31 keepPreviousData: true,
32 })
33​
34 return <div>{JSON.stringify(data)}</div>
36​
37/**
38 * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function.
39 * The codemod shouldn't transform this case, only warn the user about the manual migration.
40 */
41export const Example2 = () => {
42 const { data } = useRenamedUseQuery({
43 queryKey: ['posts'],
44 queryFn: queryFn,
45 keepPreviousData: true,
46 placeholderData: () => previousData
47 })
48​
49 return <div>{JSON.stringify(data)}</div>
51​
52/**
53 * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string.
54 * The codemod shouldn't transform this case, only warn the user about the manual migration.
55 */
56export const Example3 = () => {
57 const { data } = useRenamedUseQueries({
58 queryKey: ['posts'],
59 queryFn: queryFn,
60 keepPreviousData: true,
61 placeholderData: 'somePlaceholderData'
62 })
63​
64 return <div>{JSON.stringify(data)}</div>
66​
67/**
68 * The object expression has a `keepPreviousData` property with value `false`.
69 * The codemod shouldn't transform this case, only warn the user about the manual migration.
70 */
71export const Example4 = () => {
72 const { data } = useRenamedUseQueries({
73 queryKey: ['posts'],
74 queryFn: queryFn,
75 keepPreviousData: false,
76 })
77​
78 return <div>{JSON.stringify(data)}</div>
80​
81/**
82 * The object expression has a `keepPreviousData` property with which is an identifier.
83 * The codemod shouldn't transform this case, only warn the user about the manual migration.
84 */
85export const Example5 = () => {
86 const keepPreviousDataIdentifier = false
87 const { data } = useRenamedUseQueries({
88 queryKey: ['posts'],
89 queryFn: queryFn,
90 keepPreviousData: keepPreviousDataIdentifier,
91 placeholderData: () => previousData
92 })
93​
94 return <div>{JSON.stringify(data)}</div>
96​
97/**
98 * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform
99 * this usage.
100 */
101export const Example6 = () => {
102 const queryClient = new RenamedQueryClient()
103​
104 queryClient.setQueryDefaults(['key'], {
105 keepPreviousData: true
106 })
107​
108 useRenamedUseQueryClient().setQueryDefaults(['key'], {
109 keepPreviousData: true
110 })
111​
112 const anotherQueryClient = useRenamedUseQueryClient()
113​
114 anotherQueryClient.setQueryDefaults(['key'], {
115 keepPreviousData: true
116 })
118​
119/**
120 * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function.
121 * The codemod shouldn't transform this case, only warn the user about the manual migration.
122 */
123export const Example7 = () => {
124 const queryClient = new RenamedQueryClient()
125​
126 queryClient.setQueryDefaults(['key'], {
127 keepPreviousData: true,
128 placeholderData: () => previousData
129 })
130​
131 useRenamedUseQueryClient().setQueryDefaults(['key'], {
132 keepPreviousData: true,
133 placeholderData: () => previousData
134 })
135​
136 const anotherQueryClient = useRenamedUseQueryClient()
137​
138 anotherQueryClient.setQueryDefaults(['key'], {
139 keepPreviousData: true,
140 placeholderData: () => previousData
141 })
143​
144/**
145 * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string.
146 * The codemod shouldn't transform this case, only warn the user about the manual migration.
147 */
148export const Example8 = () => {
149 const queryClient = new RenamedQueryClient()
150​
151 queryClient.setQueryDefaults(['key'], {
152 keepPreviousData: true,
153 placeholderData: 'somePlaceholderData'
154 })
155​
156 useRenamedUseQueryClient().setQueryDefaults(['key'], {
157 keepPreviousData: true,
158 placeholderData: 'somePlaceholderData'
159 })
160​
161 const anotherQueryClient = useRenamedUseQueryClient()
162​
163 anotherQueryClient.setQueryDefaults(['key'], {
164 keepPreviousData: true,
165 placeholderData: 'somePlaceholderData'
166 })
168​
169/**
170 * The object expression has a `keepPreviousData` property with value `false`.
171 * The codemod shouldn't transform this case, only warn the user about the manual migration.
172 */
173export const Example9 = () => {
174 const queryClient = new RenamedQueryClient()
175​
176 queryClient.setQueryDefaults(['key'], {
177 keepPreviousData: false,
178 })
179​
180 useRenamedUseQueryClient().setQueryDefaults(['key'], {
181 keepPreviousData: false,
182 })
183​
184 const anotherQueryClient = useRenamedUseQueryClient()
185​
186 anotherQueryClient.setQueryDefaults(['key'], {
187 keepPreviousData: false,
188 })
190​
191/**
192 * The object expression has a `keepPreviousData` property with which is an identifier.
193 * The codemod shouldn't transform this case, only warn the user about the manual migration.
194 */
195export const Example10 = () => {
196 const queryClient = new RenamedQueryClient()
197 const keepPreviousDataIdentifier = false
198​
199 queryClient.setQueryDefaults(['key'], {
200 keepPreviousData: keepPreviousDataIdentifier,
201 placeholderData: () => previousData
202 })
203​
204 useRenamedUseQueryClient().setQueryDefaults(['key'], {
205 keepPreviousData: keepPreviousDataIdentifier,
206 placeholderData: () => previousData
207 })
208​
209 const anotherQueryClient = useRenamedUseQueryClient()
210​
211 anotherQueryClient.setQueryDefaults(['key'], {
212 keepPreviousData: keepPreviousDataIdentifier,
213 placeholderData: () => previousData
214 })
216​
217/**
218 * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform
219 * this usage.
220 */
221export const Example11 = () => {
222 new RenamedQueryClient({
223 defaultOptions: {
224 queries: {
225 keepPreviousData: true
226 }
227 }
228 })
230​
231/**
232 * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function.
233 * The codemod shouldn't transform this case, only warn the user about the manual migration.
234 */
235export const Example12 = () => {
236 new RenamedQueryClient({
237 defaultOptions: {
238 queries: {
239 keepPreviousData: true,
240 placeholderData: () => previousData
241 }
242 }
243 })
245​
246/**
247 * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string.
248 * The codemod shouldn't transform this case, only warn the user about the manual migration.
249 */
250export const Example13 = () => {
251 new RenamedQueryClient({
252 defaultOptions: {
253 queries: {
254 keepPreviousData: true,
255 placeholderData: 'somePlaceholderData'
256 }
257 }
258 })
260​
261/**
262 * The object expression has a `keepPreviousData` property with value `false`.
263 * The codemod shouldn't transform this case, only warn the user about the manual migration.
264 */
265export const Example14 = () => {
266 new RenamedQueryClient({
267 defaultOptions: {
268 queries: {
269 keepPreviousData: false,
270 }
271 }
272 })
274​
275/**
276 * The object expression has a `keepPreviousData` property with which is an identifier.
277 * The codemod shouldn't transform this case, only warn the user about the manual migration.
278 */
279export const Example15 = () => {
280 const keepPreviousDataIdentifier = false
281 new RenamedQueryClient({
282 defaultOptions: {
283 queries: {
284 keepPreviousData: keepPreviousDataIdentifier,
285 placeholderData: () => previousData
286 }
287 }
288 })