|
1 | 1 | import * as React from 'react'
|
2 | 2 | import axios from 'axios'
|
3 |
| -import { useQueries, useQuery } from '@tanstack/react-query' |
| 3 | +import { QueryClient, useQueries, useQuery, useQueryClient } from '@tanstack/react-query' |
4 | 4 |
|
5 | 5 | type Post = {
|
6 | 6 | id: number
|
@@ -53,7 +53,7 @@ export const Example3 = () => {
|
53 | 53 | queryKey: ['posts'],
|
54 | 54 | queryFn: queryFn,
|
55 | 55 | keepPreviousData: true,
|
56 |
| - placeholderData: "somePlaceholderData" |
| 56 | + placeholderData: 'somePlaceholderData' |
57 | 57 | })
|
58 | 58 |
|
59 | 59 | return <div>{JSON.stringify(data)}</div>
|
@@ -88,3 +88,197 @@ export const Example5 = () => {
|
88 | 88 |
|
89 | 89 | return <div>{JSON.stringify(data)}</div>
|
90 | 90 | }
|
| 91 | + |
| 92 | +/** |
| 93 | + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform |
| 94 | + * this usage. |
| 95 | + */ |
| 96 | +export 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 | + */ |
| 118 | +export 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 | + */ |
| 143 | +export 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 | + */ |
| 168 | +export 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 | + */ |
| 190 | +export 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 | + */ |
| 216 | +export 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 | + */ |
| 230 | +export 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 | + */ |
| 245 | +export 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 | + */ |
| 260 | +export 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 | + */ |
| 274 | +export const Example15 = () => { |
| 275 | + const keepPreviousDataIdentifier = false |
| 276 | + new QueryClient({ |
| 277 | + defaultOptions: { |
| 278 | + queries: { |
| 279 | + keepPreviousData: keepPreviousDataIdentifier, |
| 280 | + placeholderData: () => previousData |
| 281 | + } |
| 282 | + } |
| 283 | + }) |
| 284 | +} |
0 commit comments