Skip to content

Commit 7c0187e

Browse files
authored
docs: fix syntax highlighting
1 parent 1e2ee37 commit 7c0187e

File tree

1 file changed

+85
-77
lines changed

1 file changed

+85
-77
lines changed

docs/framework/react/guides/migrating-to-react-query-4.md

Lines changed: 85 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ npm install @tanstack/react-query
1717
npm install @tanstack/react-query-devtools
1818
```
1919

20-
```diff
21-
- import { useQuery } from 'react-query'
22-
- import { ReactQueryDevtools } from 'react-query/devtools'
20+
```tsx
21+
- import { useQuery } from 'react-query' // [!code --]
22+
- import { ReactQueryDevtools } from 'react-query/devtools' // [!code --]
2323
24-
+ import { useQuery } from '@tanstack/react-query'
25-
+ import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
24+
+ import { useQuery } from '@tanstack/react-query' // [!code ++]
25+
+ import { ReactQueryDevtools } from '@tanstack/react-query-devtools' // [!code ++]
2626
```
2727
2828
#### Codemod
@@ -58,15 +58,15 @@ Please note in the case of `TypeScript` you need to use `tsx` as the parser; oth
5858

5959
### Query Keys (and Mutation Keys) need to be an Array
6060

61-
In v3, Query and Mutation Keys could be a String or an Array. Internally, React Query has always worked with Array Keys only, and we've sometimes exposed this to consumers. For example, in the `queryFn`, you would always get the key as an Array to make working with [Default Query Functions](./guides/default-query-function) easier.
61+
In v3, Query and Mutation Keys could be a String or an Array. Internally, React Query has always worked with Array Keys only, and we've sometimes exposed this to consumers. For example, in the `queryFn`, you would always get the key as an Array to make working with [Default Query Functions](../default-query-function) easier.
6262

63-
However, we have not followed this concept through to all apis. For example, when using the `predicate` function on [Query Filters](./guides/filters) you would get the raw Query Key. This makes it difficult to work with such functions if you use Query Keys that are mixed Arrays and Strings. The same was true when using global callbacks.
63+
However, we have not followed this concept through to all apis. For example, when using the `predicate` function on [Query Filters](../filters) you would get the raw Query Key. This makes it difficult to work with such functions if you use Query Keys that are mixed Arrays and Strings. The same was true when using global callbacks.
6464

6565
To streamline all apis, we've decided to make all keys Arrays only:
6666

67-
```diff
68-
- useQuery('todos', fetchTodos)
69-
+ useQuery(['todos'], fetchTodos)
67+
```tsx
68+
;-useQuery('todos', fetchTodos) + // [!code --]
69+
useQuery(['todos'], fetchTodos) // [!code ++]
7070
```
7171

7272
#### Codemod
@@ -100,41 +100,49 @@ Please note in the case of `TypeScript` you need to use `tsx` as the parser; oth
100100

101101
### The idle state has been removed
102102

103-
With the introduction of the new [fetchStatus](./guides/queries#fetchstatus) for better offline support, the `idle` state became irrelevant, because `fetchStatus: 'idle'` captures the same state better. For more information, please read [Why two different states](./guides/queries#why-two-different-states).
103+
With the introduction of the new [fetchStatus](../queries#fetchstatus) for better offline support, the `idle` state became irrelevant, because `fetchStatus: 'idle'` captures the same state better. For more information, please read [Why two different states](../queries#why-two-different-states).
104104

105105
This will mostly affect `disabled` queries that don't have any `data` yet, as those were in `idle` state before:
106106

107-
```diff
108-
- status: 'idle'
109-
+ status: 'loading'
110-
+ fetchStatus: 'idle'
107+
```tsx
108+
- status: 'idle' // [!code --]
109+
+ status: 'loading' // [!code ++]
110+
+ fetchStatus: 'idle' // [!code ++]
111111
```
112112

113-
Also, have a look at [the guide on dependent queries](./guides/dependent-queries)
113+
Also, have a look at [the guide on dependent queries](../dependent-queries)
114114

115115
#### disabled queries
116116

117117
Due to this change, disabled queries (even temporarily disabled ones) will start in `loading` state. To make migration easier, especially for having a good flag to know when to display a loading spinner, you can check for `isInitialLoading` instead of `isLoading`:
118118

119-
```diff
120-
- isLoading
121-
+ isInitialLoading
119+
```tsx
120+
;-isLoading + // [!code --]
121+
isInitialLoading // [!code ++]
122122
```
123123

124-
See also the guide on [disabling queries](./guides/disabling-queries#isInitialLoading)
124+
See also the guide on [disabling queries](../disabling-queries#isInitialLoading)
125125

126126
### new API for `useQueries`
127127

128128
The `useQueries` hook now accepts an object with a `queries` prop as its input. The value of the `queries` prop is an array of queries (this array is identical to what was passed into `useQueries` in v3).
129129

130-
```diff
131-
- useQueries([{ queryKey1, queryFn1, options1 }, { queryKey2, queryFn2, options2 }])
132-
+ useQueries({ queries: [{ queryKey1, queryFn1, options1 }, { queryKey2, queryFn2, options2 }] })
130+
```tsx
131+
;-useQueries([
132+
{ queryKey1, queryFn1, options1 },
133+
{ queryKey2, queryFn2, options2 },
134+
]) + // [!code --]
135+
useQueries({
136+
queries: [
137+
{ queryKey1, queryFn1, options1 },
138+
{ queryKey2, queryFn2, options2 },
139+
],
140+
}) // [!code ++]
133141
```
134142

135143
### Undefined is an illegal cache value for successful queries
136144

137-
In order to make bailing out of updates possible by returning `undefined`, we had to make `undefined` an illegal cache value. This is in-line with other concepts of react-query, for example, returning `undefined` from the [initialData function](guides/initial-query-data#initial-data-function) will also _not_ set data.
145+
In order to make bailing out of updates possible by returning `undefined`, we had to make `undefined` an illegal cache value. This is in-line with other concepts of react-query, for example, returning `undefined` from the [initialData function](../initial-query-data#initial-data-function) will also _not_ set data.
138146

139147
Further, it is an easy bug to produce `Promise<void>` by adding logging in the queryFn:
140148

@@ -148,7 +156,7 @@ This is now disallowed on type level; at runtime, `undefined` will be transforme
148156

149157
### Queries and mutations, per default, need network connection to run
150158

151-
Please read the [New Features announcement](#proper-offline-support) about online / offline support, and also the dedicated page about [Network mode](./guides/network-mode)
159+
Please read the [New Features announcement](#proper-offline-support) about online / offline support, and also the dedicated page about [Network mode](../network-mode)
152160

153161
Even though React Query is an Async State Manager that can be used for anything that produces a Promise, it is most often used for data fetching in combination with data fetching libraries. That is why, per default, queries and mutations will be `paused` if there is no network connection. If you want to opt-in to the previous behavior, you can globally set `networkMode: offlineFirst` for both queries and mutations:
154162

@@ -209,7 +217,7 @@ queryClient.refetchQueries({ queryKey: ['todos'] }, { cancelRefetch: false })
209217
210218
### Query Filters
211219

212-
A [query filter](./guides/filters) is an object with certain conditions to match a query. Historically, the filter options have mostly been a combination of boolean flags. However, combining those flags can lead to impossible states. Specifically:
220+
A [query filter](../filters) is an object with certain conditions to match a query. Historically, the filter options have mostly been a combination of boolean flags. However, combining those flags can lead to impossible states. Specifically:
213221

214222
```
215223
active?: boolean
@@ -224,17 +232,17 @@ Those flags don't work well when used together, because they are mutually exclus
224232

225233
With v4, those filters have been combined into a single filter to better show the intent:
226234

227-
```diff
228-
- active?: boolean
229-
- inactive?: boolean
230-
+ type?: 'active' | 'inactive' | 'all'
235+
```tsx
236+
- active?: boolean // [!code --]
237+
- inactive?: boolean // [!code --]
238+
+ type?: 'active' | 'inactive' | 'all' // [!code ++]
231239
```
232240

233241
The filter defaults to `all`, and you can choose to only match `active` or `inactive` queries.
234242

235243
#### refetchActive / refetchInactive
236244

237-
[queryClient.invalidateQueries](./reference/QueryClient#queryclientinvalidatequeries) had two additional, similar flags:
245+
[queryClient.invalidateQueries](../../../../reference/QueryClient/#queryclientinvalidatequeries) had two additional, similar flags:
238246

239247
```
240248
refetchActive: Boolean
@@ -249,10 +257,10 @@ refetchInactive: Boolean
249257

250258
For the same reason, those have also been combined:
251259

252-
```diff
253-
- refetchActive?: boolean
254-
- refetchInactive?: boolean
255-
+ refetchType?: 'active' | 'inactive' | 'all' | 'none'
260+
```tsx
261+
- refetchActive?: boolean // [!code --]
262+
- refetchInactive?: boolean // [!code --]
263+
+ refetchType?: 'active' | 'inactive' | 'all' | 'none' // [!code ++]
256264
```
257265

258266
This flag defaults to `active` because `refetchActive` defaulted to `true`. This means we also need a way to tell `invalidateQueries` to not refetch at all, which is why a fourth option (`none`) is also allowed here.
@@ -272,43 +280,43 @@ React.useEffect(() => mySideEffectHere(data), [data])
272280

273281
### `persistQueryClient` and the corresponding persister plugins are no longer experimental and have been renamed
274282

275-
The plugins `createWebStoragePersistor` and `createAsyncStoragePersistor` have been renamed to [`createSyncStoragePersister`](./plugins/createSyncStoragePersister) and [`createAsyncStoragePersister`](./plugins/createAsyncStoragePersister) respectively. The interface `Persistor` in `persistQueryClient` has also been renamed to `Persister`. Checkout [this stackexchange](https://english.stackexchange.com/questions/206893/persister-or-persistor) for the motivation of this change.
283+
The plugins `createWebStoragePersistor` and `createAsyncStoragePersistor` have been renamed to [`createSyncStoragePersister`](../../plugins/createSyncStoragePersister) and [`createAsyncStoragePersister`](../../plugins/createAsyncStoragePersister) respectively. The interface `Persistor` in `persistQueryClient` has also been renamed to `Persister`. Checkout [this stackexchange](https://english.stackexchange.com/questions/206893/persister-or-persistor) for the motivation of this change.
276284

277285
Since these plugins are no longer experimental, their import paths have also been updated:
278286

279-
```diff
280-
- import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
281-
- import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental'
282-
- import { createAsyncStoragePersistor } from 'react-query/createAsyncStoragePersistor-experimental'
287+
```tsx
288+
- import { persistQueryClient } from 'react-query/persistQueryClient-experimental' // [!code --]
289+
- import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental' // [!code --]
290+
- import { createAsyncStoragePersistor } from 'react-query/createAsyncStoragePersistor-experimental' // [!code --]
283291
284-
+ import { persistQueryClient } from '@tanstack/react-query-persist-client'
285-
+ import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
286-
+ import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
292+
+ import { persistQueryClient } from '@tanstack/react-query-persist-client' // [!code ++]
293+
+ import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister' // [!code ++]
294+
+ import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister' // [!code ++]
287295
```
288296
289297
### The `cancel` method on promises is no longer supported
290298
291-
The [old `cancel` method](./guides/query-cancellation#old-cancel-function) that allowed you to define a `cancel` function on promises, which was then used by the library to support query cancellation, has been removed. We recommend to use the [newer API](./guides/query-cancellation) (introduced with v3.30.0) for query cancellation that uses the [`AbortController` API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) internally and provides you with an [`AbortSignal` instance](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for your query function to support query cancellation.
299+
The [old `cancel` method](../query-cancellation#old-cancel-function) that allowed you to define a `cancel` function on promises, which was then used by the library to support query cancellation, has been removed. We recommend to use the [newer API](../query-cancellation) (introduced with v3.30.0) for query cancellation that uses the [`AbortController` API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) internally and provides you with an [`AbortSignal` instance](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for your query function to support query cancellation.
292300
293301
### TypeScript
294302
295303
Types now require using TypeScript v4.1 or greater
296304
297305
### Supported Browsers
298306
299-
As of v4, React Query is optimized for modern browsers. We have updated our browserslist to produce a more modern, performant and smaller bundle. You can read about the requirements [here](./installation#requirements).
307+
As of v4, React Query is optimized for modern browsers. We have updated our browserslist to produce a more modern, performant and smaller bundle. You can read about the requirements [here](../../installation#requirements).
300308
301309
### `setLogger` is removed
302310
303311
It was possible to change the logger globally by calling `setLogger`. In v4, that function is replaced with an optional field when creating a `QueryClient`.
304312
305-
```diff
306-
- import { QueryClient, setLogger } from 'react-query';
307-
+ import { QueryClient } from '@tanstack/react-query';
313+
```tsx
314+
- import { QueryClient, setLogger } from 'react-query'; // [!code --]
315+
+ import { QueryClient } from '@tanstack/react-query'; // [!code ++]
308316

309-
- setLogger(customLogger)
310-
- const queryClient = new QueryClient();
311-
+ const queryClient = new QueryClient({ logger: customLogger })
317+
- setLogger(customLogger) // [!code --]
318+
- const queryClient = new QueryClient(); // [!code --]
319+
+ const queryClient = new QueryClient({ logger: customLogger }) // [!code ++]
312320
```
313321

314322
### No _default_ manual Garbage Collection server-side
@@ -334,13 +342,13 @@ Subscribing manually to the `QueryCache` has always given you a `QueryCacheNotif
334342

335343
#### QueryCacheNotifyEvent
336344

337-
```diff
338-
- type: 'queryAdded'
339-
+ type: 'added'
340-
- type: 'queryRemoved'
341-
+ type: 'removed'
342-
- type: 'queryUpdated'
343-
+ type: 'updated'
345+
```tsx
346+
- type: 'queryAdded' // [!code --]
347+
+ type: 'added' // [!code ++]
348+
- type: 'queryRemoved' // [!code --]
349+
+ type: 'removed' // [!code ++]
350+
- type: 'queryUpdated' // [!code --]
351+
+ type: 'updated' // [!code ++]
344352
```
345353

346354
#### MutationCacheNotifyEvent
@@ -353,26 +361,26 @@ The `MutationCacheNotifyEvent` uses the same types as the `QueryCacheNotifyEvent
353361

354362
With version [3.22.0](https://github.com/tannerlinsley/react-query/releases/tag/v3.22.0), hydration utilities moved into the React Query core. With v3, you could still use the old exports from `react-query/hydration`, but these exports have been removed with v4.
355363

356-
```diff
357-
- import { dehydrate, hydrate, useHydrate, Hydrate } from 'react-query/hydration'
358-
+ import { dehydrate, hydrate, useHydrate, Hydrate } from '@tanstack/react-query'
364+
```tsx
365+
- import { dehydrate, hydrate, useHydrate, Hydrate } from 'react-query/hydration' // [!code --]
366+
+ import { dehydrate, hydrate, useHydrate, Hydrate } from '@tanstack/react-query' // [!code ++]
359367
```
360368
361369
### Removed undocumented methods from the `queryClient`, `query` and `mutation`
362370
363371
The methods `cancelMutatations` and `executeMutation` on the `QueryClient` were undocumented and unused internally, so we removed them. Since it was just a wrapper around a method available on the `mutationCache`, you can still use the functionality of `executeMutation`
364372
365-
```diff
366-
- executeMutation<
367-
- TData = unknown,
368-
- TError = unknown,
369-
- TVariables = void,
370-
- TContext = unknown
371-
- >(
372-
- options: MutationOptions<TData, TError, TVariables, TContext>
373-
- ): Promise<TData> {
374-
- return this.mutationCache.build(this, options).execute()
375-
- }
373+
```tsx
374+
- executeMutation< // [!code --]
375+
- TData = unknown, // [!code --]
376+
- TError = unknown, // [!code --]
377+
- TVariables = void, // [!code --]
378+
- TContext = unknown // [!code --]
379+
- >( // [!code --]
380+
- options: MutationOptions<TData, TError, TVariables, TContext> // [!code --]
381+
- ): Promise<TData> { // [!code --]
382+
- return this.mutationCache.build(this, options).execute() // [!code --]
383+
- } // [!code --]
376384
```
377385

378386
Additionally, `query.setDefaultOptions` was removed because it was also unused. `mutation.cancel` was removed because it didn't actually cancel the outgoing request.
@@ -389,9 +397,9 @@ With the renamed directory this no longer is an issue.
389397

390398
If you were importing anything from `'react-query/react'` directly in your project (as opposed to just `'react-query'`), then you need to update your imports:
391399

392-
```diff
393-
- import { QueryClientProvider } from 'react-query/react';
394-
+ import { QueryClientProvider } from '@tanstack/react-query/reactjs';
400+
```tsx
401+
- import { QueryClientProvider } from 'react-query/react'; // [!code --]
402+
+ import { QueryClientProvider } from '@tanstack/react-query/reactjs'; // [!code ++]
395403
```
396404

397405
## New Features 🚀
@@ -411,15 +419,15 @@ In v3, React Query has always fired off queries and mutations, but then taken th
411419
- You are offline and want to fire off a query that doesn't necessarily need network connection (because you _can_ use React Query for something other than data fetching), but it fails for some other reason. That query will now be paused until you go online again.
412420
- Window focus refetching didn't do anything at all if you were offline.
413421

414-
With v4, React Query introduces a new `networkMode` to tackle all these issues. Please read the dedicated page about the new [Network mode](./guides/network-mode) for more information.
422+
With v4, React Query introduces a new `networkMode` to tackle all these issues. Please read the dedicated page about the new [Network mode](../network-mode) for more information.
415423

416424
### Tracked Queries per default
417425

418426
React Query defaults to "tracking" query properties, which should give you a nice boost in render optimization. The feature has existed since [v3.6.0](https://github.com/tannerlinsley/react-query/releases/tag/v3.6.0) and has now become the default behavior with v4.
419427

420428
### Bailing out of updates with setQueryData
421429

422-
When using the [functional updater form of setQueryData](./reference/QueryClient#queryclientsetquerydata), you can now bail out of the update by returning `undefined`. This is helpful if `undefined` is given to you as `previousValue`, which means that currently, no cached entry exists and you don't want to / cannot create one, like in the example of toggling a todo:
430+
When using the [functional updater form of setQueryData](../../../../reference/QueryClient/#queryclientsetquerydata), you can now bail out of the update by returning `undefined`. This is helpful if `undefined` is given to you as `previousValue`, which means that currently, no cached entry exists and you don't want to / cannot create one, like in the example of toggling a todo:
423431

424432
```tsx
425433
queryClient.setQueryData(['todo', id], (previousTodo) =>

0 commit comments

Comments
 (0)