Skip to content

Commit 17df67a

Browse files
committed
add migration note
1 parent b87e317 commit 17df67a

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

.babelrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module.exports = {
2222
plugins: [
2323
cjs && ['@babel/transform-modules-commonjs', { loose }],
2424
es && ['babel-plugin-add-import-extension', { extension: 'mjs' }],
25-
[
25+
// no runtime for umd builds
26+
BABEL_ENV && [
2627
'@babel/transform-runtime',
2728
{
2829
version: require('./package.json').dependencies[

docs/src/pages/guides/migrating-to-react-query-4.md

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ title: Migrating to React Query 4
55

66
## Breaking Changes
77

8+
### ESM Support
9+
10+
React Query now supports [package.json `"exports"`](https://nodejs.org/api/packages.html#exports) and is fully compatible with Node's native resolution for both CommonJS and ESM. We don't expect this to be a breaking change for most users, but this restricts the files you can import into your project to only the entry points we officially support.
11+
812
### Query Keys (and Mutation Keys) need to be an Array
913

1014
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.
@@ -222,12 +226,12 @@ Even though React Query is an Async State Manager that can be used for anything
222226
new QueryClient({
223227
defaultOptions: {
224228
queries: {
225-
networkMode: 'offlineFirst'
229+
networkMode: 'offlineFirst',
226230
},
227231
mutations: {
228-
networkmode: 'offlineFirst'
229-
}
230-
}
232+
networkmode: 'offlineFirst',
233+
},
234+
},
231235
})
232236
```
233237

@@ -240,7 +244,6 @@ The `useQueries` hook now accepts an object with a `queries` prop as its input.
240244
+ useQueries({ queries: [{ queryKey1, queryFn1, options1 }, { queryKey2, queryFn2, options2 }] })
241245
```
242246

243-
244247
### Removed undocumented methods from the `queryClient`, `query` and `mutation`
245248

246249
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`
@@ -289,10 +292,7 @@ In order to make bailing out of updates possible by returning `undefined`, we ha
289292
Further, it is an easy bug to produce `Promise<void>` by adding logging in the queryFn:
290293

291294
```js
292-
useQuery(
293-
['key'],
294-
() => axios.get(url).then(result => console.log(result.data))
295-
)
295+
useQuery(['key'], () => axios.get(url).then(result => console.log(result.data)))
296296
```
297297

298298
This is now disallowed on type level; at runtime, `undefined` will be transformed to a _failed Promise_, which means you will get an `error`, which will also be logged to the console in development mode.
@@ -349,19 +349,18 @@ React Query defaults to "tracking" query properties, which should give you a nic
349349
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:
350350

351351
```js
352-
queryClient.setQueryData(
353-
['todo', id],
354-
(previousTodo) => previousTodo ? { ...previousTodo, done: true } : undefined
352+
queryClient.setQueryData(['todo', id], previousTodo =>
353+
previousTodo ? { ...previousTodo, done: true } : undefined
355354
)
356-
```
355+
```
357356

358-
### Custom Contexts for Multiple Providers
357+
### Custom Contexts for Multiple Providers
359358

360359
Custom contexts can now be specified to pair hooks with their matching `Provider`. This is critical when there may be multiple React Query `Provider` instances in the component tree, and you need to ensure your hook uses the correct `Provider` instance.
361360

362361
An example:
363362

364-
1) Create a data package.
363+
1. Create a data package.
365364

366365
```tsx
367366
// Our first data package: @my-scope/container-data
@@ -375,42 +374,47 @@ export const useUser = () => {
375374
})
376375
}
377376

378-
export const ContainerDataProvider = ({ children }: { children: React.ReactNode}) => {
377+
export const ContainerDataProvider = ({
378+
children,
379+
}: {
380+
children: React.ReactNode
381+
}) => {
379382
return (
380383
<QueryClientProvider client={queryClient} context={context}>
381384
{children}
382385
</QueryClientProvider>
383386
)
384387
}
385-
386388
```
387389

388-
2) Create a second data package.
390+
2. Create a second data package.
389391

390392
```tsx
391393
// Our second data package: @my-scope/my-component-data
392394

393395
const context = React.createContext<QueryClient | undefined>(undefined)
394396
const queryClient = new QueryClient()
395397

396-
397398
export const useItems = () => {
398399
return useQuery(ITEMS_KEY, ITEMS_FETCHER, {
399400
context,
400401
})
401402
}
402403

403-
export const MyComponentDataProvider = ({ children }: { children: React.ReactNode}) => {
404+
export const MyComponentDataProvider = ({
405+
children,
406+
}: {
407+
children: React.ReactNode
408+
}) => {
404409
return (
405410
<QueryClientProvider client={queryClient} context={context}>
406411
{children}
407412
</QueryClientProvider>
408413
)
409414
}
410-
411415
```
412416

413-
3) Use these two data packages in your application.
417+
3. Use these two data packages in your application.
414418

415419
```tsx
416420
// Our application

rollup.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ const extensions = ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts', '.tsx']
4646

4747
const babelConfig = {
4848
extensions,
49-
babelHelpers: 'runtime',
50-
exclude: 'node_modules/**',
49+
babelHelpers: 'bundled',
5150
}
5251

5352
const resolveConfig = { extensions }

0 commit comments

Comments
 (0)