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