Skip to content

Commit 05e26ae

Browse files
committed
Merge branch 'beta' of https://github.com/tannerlinsley/react-query into fix/query-function-data-dont-accept-promise-undefined
2 parents b9168c2 + fdbc002 commit 05e26ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+723
-1464
lines changed

.babelrc.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { NODE_ENV, BABEL_ENV } = process.env
22
const cjs = NODE_ENV === 'test' || BABEL_ENV === 'commonjs'
3+
const es = BABEL_ENV === 'es'
34
const loose = true
45

56
module.exports = {
@@ -20,10 +21,11 @@ module.exports = {
2021
],
2122
plugins: [
2223
cjs && ['@babel/transform-modules-commonjs', { loose }],
23-
[
24+
es && ['babel-plugin-add-import-extension', { extension: 'mjs' }],
25+
// no runtime for umd builds
26+
BABEL_ENV && [
2427
'@babel/transform-runtime',
2528
{
26-
useESModules: !cjs,
2729
version: require('./package.json').dependencies[
2830
'@babel/runtime'
2931
].replace(/^[^0-9]*/, ''),

.github/workflows/test-and-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
runs-on: ubuntu-latest
1818
strategy:
1919
matrix:
20-
node: [12, 14, 16]
20+
node: [14, 16]
2121
react: [17, 18]
2222
steps:
2323
- uses: actions/checkout@v2

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Enjoy this library? Try the entire [TanStack](https://tanstack.com)! [React Tabl
3030

3131
## Visit [react-query.tanstack.com](https://react-query.tanstack.com) for docs, guides, API and more!
3232

33-
Still on **React Query v2**? No problem! Check out the v2 docs here: https://react-query-v2.tanstack.com/.
33+
Still on **React Query v2**? No problem! Check out the v2 docs here: https://react-query-v2.tanstack.com/. <br />
34+
Would you like to try **React Query v4beta**? Check out the v4 beta docs here: https://react-query-beta.tanstack.com/.
3435
## Quick Features
3536

3637
- Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../lib/broadcastQueryClient-experimental'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../lib/broadcastQueryClient-experimental/index')

core/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../lib/core'

core/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../lib/core/index')

core/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../lib/createAsyncStoragePersister'

createAsyncStoragePersister/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../lib/createAsyncStoragePersister/index')

createAsyncStoragePersister/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

createWebStoragePersister/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../lib/createWebStoragePersister'

createWebStoragePersister/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../lib/createWebStoragePersister/index')

createWebStoragePersister/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

devtools/development/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../../lib/devtools'

devtools/development/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../lib/devtools/index')

devtools/development/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

devtools/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../lib/devtools'

devtools/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
if (process.env.NODE_ENV !== 'development') {
2-
module.exports = {
3-
ReactQueryDevtools: function () {
4-
return null
5-
},
6-
ReactQueryDevtoolsPanel: function () {
7-
return null
8-
},
9-
}
2+
module.exports = require('../lib/devtools/noop')
103
} else {
11-
module.exports = require('./development')
4+
module.exports = require('../lib/devtools/index')
125
}

devtools/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/src/components/LayoutDocs.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export const LayoutDocs = props => {
119119
<p className='text-sm'>
120120
Fast track your learning and {' '}
121121
<a
122-
href="https://ui.dev/checkout/react-query?from=tanstack"
122+
href="https://ui.dev/react-query?from=tanstack"
123123
className="text-blue-600 font-semibold transition-colors duration-150 ease-out"
124124
>
125125
take the offical course ↗️
@@ -128,14 +128,19 @@ export const LayoutDocs = props => {
128128
</div>
129129
<div className="mt-12 relative">
130130
<h4 className="font-semibold uppercase text-sm mb-2 mt-2 text-gray-500">
131-
Subscribe to Bytes
131+
Subscribe to{' '}
132+
<a
133+
className="text-blue-600"
134+
href="https://bytes.dev?r=tanstack"
135+
>
136+
Bytes
137+
</a>
132138
</h4>
133139
<p className="mt-4 text-sm leading-6 mb-4">
134-
The best JavaScript newsletter! Delivered every
135-
Monday to over 76,000 devs.
140+
Your weekly dose of JavaScript news. Delivered every
141+
Monday to over 80,000 devs, for free.
136142
</p>
137143
<BytesForm />
138-
139144
</div>
140145
</div>
141146
</div>

docs/src/components/Nav.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const Nav = () => (
3737
</div>
3838
<div>
3939
<a
40-
href="https://learn.tanstack.com/p/react-query-essentials"
40+
href="https://ui.dev/react-query?from=tanstack"
4141
target="_blank"
4242
className="leading-6 font-medium"
4343
>

docs/src/components/PPPBanner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function PPPBanner() {
4949
Course with code{' '}
5050
<a
5151
className="underline cursor-pointer"
52-
href={`/checkout/react-query?from=tanstack&coupon_code=${data.coupon}`}
52+
href={`https://ui.dev/react-query?from=tanstack&coupon_code=${data.coupon}`}
5353
>
5454
<strong>{data.coupon}</strong>
5555
</a>

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

docs/src/pages/guides/query-cancellation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ const query = useQuery(['todos'], ({ signal }) => {
127127
You might want to cancel a query manually. For example, if the request takes a long time to finish, you can allow the user to click a cancel button to stop the request. To do this, you just need to call `queryClient.cancelQueries(key)`, which will cancel the query and revert it back to its previous state. If `promise.cancel` is available, or you have consumed the `signal` passed to the query function, React Query will additionally also cancel the Promise.
128128
129129
```js
130-
const query = useQuery(['todos'], await ({ signal }) => {
131-
const resp = fetch('/todos', { signal })
130+
const query = useQuery(['todos'], async ({ signal }) => {
131+
const resp = await fetch('/todos', { signal })
132132
return resp.json()
133133
})
134134

docs/src/pages/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ const Home = ({ sponsors }) => {
7474
<p>
7575
Want to skip the docs?{' '}
7676
<a
77-
href="https://ui.dev/checkout/react-query?from=tanstack"
77+
href="https://ui.dev/react-query?from=tanstack"
7878
className="text-blue-600 font-semibold transition-colors duration-150 ease-out"
7979
>
80-
Take the offical course
80+
Take the official course
8181
</a>
8282
</p>
8383
</div>
@@ -177,7 +177,7 @@ const Home = ({ sponsors }) => {
177177
</span>
178178
</div>
179179
<a
180-
href="https://ui.dev/checkout/react-query?from=tanstack"
180+
href="https://ui.dev/react-query?from=tanstack"
181181
target="_blank"
182182
className="inline-block mt-8 rounded shadow-lg bg-coral text-white font-bold text-xl px-4 py-3"
183183
>
@@ -375,7 +375,7 @@ const Home = ({ sponsors }) => {
375375
</h2>
376376
<div className="mt-8 flex lg:flex-shrink-0 md:mt-0">
377377
<div className="inline-flex rounded-md shadow">
378-
<a href="https://ui.dev/checkout/react-query?from=tanstack" className="inline-flex items-center justify-center text-center px-5 py-3 border border-transparent text-base leading-6 font-medium rounded-md text-white bg-coral hover:bg-coral-light focus:outline-none focus:shadow-outline transition duration-150 ease-in-out">
378+
<a href="https://ui.dev/react-query?from=tanstack" className="inline-flex items-center justify-center text-center px-5 py-3 border border-transparent text-base leading-6 font-medium rounded-md text-white bg-coral hover:bg-coral-light focus:outline-none focus:shadow-outline transition duration-150 ease-in-out">
379379
Take the course
380380
</a>
381381
</div>

docs/src/pages/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ function Example() {
8484

8585
## You talked me into it, so what now?
8686

87-
- Consider taking the official [React Query Course](https://ui.dev/checkout/react-query?from=tanstack) (or buying it for your whole team!)
87+
- Consider taking the official [React Query Course](https://ui.dev/react-query?from=tanstack) (or buying it for your whole team!)
8888
- Learn React Query at your own pace with our amazingly thorough [Walkthrough Guide](../installation) and [API Reference](../reference/useQuery)

docs/src/pages/plugins/persistQueryClient.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ ReactDOM.createRoot(rootElement).render(
220220
- will be called when the initial restore is finished
221221
- can be used to [resumePausedMutations](../reference/QueryClient#queryclientresumepausedmutations)
222222

223+
### useIsRestoring
224+
225+
If you are using the `PersistQueryClientProvider`, you can also use the `useIsRestoring` hook alongside it to check if a restore is currently in progress. `useQuery` and friends also check this internally to avoid race conditions between the restore and mounting queries.
226+
223227
## Persisters
224228

225229
### Persisters Interface

docs/src/pages/react-native.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,19 @@ import React from 'react'
5555
import { useFocusEffect } from '@react-navigation/native'
5656

5757
export function useRefreshOnFocus<T>(refetch: () => Promise<T>) {
58-
const enabledRef = React.useRef(false)
58+
const firstTimeRef = React.useRef(true)
5959

6060
useFocusEffect(
6161
React.useCallback(() => {
62-
if (enabledRef.current) {
63-
refetch()
64-
} else {
65-
enabledRef.current = true
62+
if (firstTimeRef.current) {
63+
firstTimeRef.current = false;
64+
return;
6665
}
66+
67+
refetch()
6768
}, [refetch])
6869
)
6970
}
7071
```
72+
73+
In the above code, `refetch` is skipped the first time because `useFocusEffect` calls our callback on mount in addition to screen focus.

0 commit comments

Comments
 (0)