Skip to content

Commit 35dfe5f

Browse files
committed
merge branch master into beta
2 parents 7bba5a9 + fab76e6 commit 35dfe5f

File tree

14 files changed

+222
-60
lines changed

14 files changed

+222
-60
lines changed

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!)

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/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/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.

docs/src/pages/reference/useQuery.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,27 @@ const result = useQuery({
114114
- `refetchIntervalInBackground: boolean`
115115
- Optional
116116
- If set to `true`, queries that are set to continuously refetch with a `refetchInterval` will continue to refetch while their tab/window is in the background
117-
- `refetchOnMount: boolean | "always"`
117+
- `refetchOnMount: boolean | "always" | ((query: Query) => boolean | "always")`
118118
- Optional
119119
- Defaults to `true`
120120
- If set to `true`, the query will refetch on mount if the data is stale.
121121
- If set to `false`, the query will not refetch on mount.
122122
- If set to `"always"`, the query will always refetch on mount.
123-
- `refetchOnWindowFocus: boolean | "always"`
123+
- If set to a function, the function will be executed with the query to compute the value
124+
- `refetchOnWindowFocus: boolean | "always" | ((query: Query) => boolean | "always")`
124125
- Optional
125126
- Defaults to `true`
126127
- If set to `true`, the query will refetch on window focus if the data is stale.
127128
- If set to `false`, the query will not refetch on window focus.
128129
- If set to `"always"`, the query will always refetch on window focus.
129-
- `refetchOnReconnect: boolean | "always"`
130+
- If set to a function, the function will be executed with the query to compute the value
131+
- `refetchOnReconnect: boolean | "always" | ((query: Query) => boolean | "always")`
130132
- Optional
131133
- Defaults to `true`
132134
- If set to `true`, the query will refetch on reconnect if the data is stale.
133135
- If set to `false`, the query will not refetch on reconnect.
134136
- If set to `"always"`, the query will always refetch on reconnect.
137+
- If set to a function, the function will be executed with the query to compute the value
135138
- `notifyOnChangeProps: string[] | "all"`
136139
- Optional
137140
- If set, the component will only re-render if any of the listed properties change.

src/core/queryObserver.ts

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,19 @@ export class QueryObserver<
120120
}
121121

122122
shouldFetchOnReconnect(): boolean {
123-
return shouldFetchOnReconnect(this.currentQuery, this.options)
123+
return shouldFetchOn(
124+
this.currentQuery,
125+
this.options,
126+
this.options.refetchOnReconnect
127+
)
124128
}
125129

126130
shouldFetchOnWindowFocus(): boolean {
127-
return shouldFetchOnWindowFocus(this.currentQuery, this.options)
131+
return shouldFetchOn(
132+
this.currentQuery,
133+
this.options,
134+
this.options.refetchOnWindowFocus
135+
)
128136
}
129137

130138
destroy(): void {
@@ -694,47 +702,30 @@ function shouldLoadOnMount(
694702
)
695703
}
696704

697-
function shouldRefetchOnMount(
698-
query: Query<any, any, any, any>,
699-
options: QueryObserverOptions<any, any, any, any>
700-
): boolean {
701-
return (
702-
options.enabled !== false &&
703-
query.state.dataUpdatedAt > 0 &&
704-
(options.refetchOnMount === 'always' ||
705-
(options.refetchOnMount !== false && isStale(query, options)))
706-
)
707-
}
708-
709705
function shouldFetchOnMount(
710706
query: Query<any, any, any, any>,
711707
options: QueryObserverOptions<any, any, any, any, any>
712708
): boolean {
713709
return (
714-
shouldLoadOnMount(query, options) || shouldRefetchOnMount(query, options)
710+
shouldLoadOnMount(query, options) ||
711+
(query.state.dataUpdatedAt > 0 &&
712+
shouldFetchOn(query, options, options.refetchOnMount))
715713
)
716714
}
717715

718-
function shouldFetchOnReconnect(
716+
function shouldFetchOn(
719717
query: Query<any, any, any, any>,
720-
options: QueryObserverOptions<any, any, any, any, any>
721-
): boolean {
722-
return (
723-
options.enabled !== false &&
724-
(options.refetchOnReconnect === 'always' ||
725-
(options.refetchOnReconnect !== false && isStale(query, options)))
726-
)
727-
}
728-
729-
function shouldFetchOnWindowFocus(
730-
query: Query<any, any, any, any>,
731-
options: QueryObserverOptions<any, any, any, any, any>
732-
): boolean {
733-
return (
734-
options.enabled !== false &&
735-
(options.refetchOnWindowFocus === 'always' ||
736-
(options.refetchOnWindowFocus !== false && isStale(query, options)))
737-
)
718+
options: QueryObserverOptions<any, any, any, any, any>,
719+
field: typeof options['refetchOnMount'] &
720+
typeof options['refetchOnWindowFocus'] &
721+
typeof options['refetchOnReconnect']
722+
) {
723+
if (options.enabled !== false) {
724+
const value = typeof field === 'function' ? field(query) : field
725+
726+
return value === 'always' || (value !== false && isStale(query, options))
727+
}
728+
return false
738729
}
739730

740731
function shouldFetchOptionally(

0 commit comments

Comments
 (0)