Skip to content

Commit 8025dbf

Browse files
committed
Merge remote-tracking branch 'tannerlinsley/main' into beta
# Conflicts: # examples/react/auto-refetching/package.json # examples/react/basic-graphql-request/package.json # examples/react/basic-typescript/package.json # examples/react/basic/package.json # examples/react/custom-hooks/package.json # examples/react/default-query-function/package.json # examples/react/focus-refetching/package.json # examples/react/load-more-infinite-scroll/package.json # examples/react/nextjs/package.json # examples/react/offline/package.json # examples/react/optimistic-updates-typescript/package.json # examples/react/optimistic-updates/package.json # examples/react/pagination/package.json # examples/react/playground/package.json # examples/react/prefetching/package.json # examples/react/react-native/package.json # examples/react/rick-morty/package.json # examples/react/simple/package.json # examples/react/star-wars/package.json # examples/react/suspense/package.json # package-lock.json # packages/query-broadcast-client-experimental/package.json # packages/query-broadcast-client-experimental/yarn.lock # packages/query-core/package.json # packages/react-query/package.json
2 parents 4a4543c + 262f83f commit 8025dbf

File tree

162 files changed

+2700
-2734
lines changed

Some content is hidden

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

162 files changed

+2700
-2734
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ Hooks for fetching, caching and updating asynchronous data in React
1212
<img src="https://github.com/tannerlinsley/react-query/workflows/react-query%20tests/badge.svg" />
1313
</a><a href="https://npmjs.com/package/react-query" target="\_parent">
1414
<img alt="" src="https://img.shields.io/npm/dm/react-query.svg" />
15-
</a><a href="https://bundlephobia.com/result?p=react-query@latest" target="\_parent">
16-
<img alt="" src="https://badgen.net/bundlephobia/minzip/react-query@latest" />
15+
</a><a href="https://bundlephobia.com/package/@tanstack/react-query@latest" target="\_parent">
16+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/react-query" />
1717
</a><a href="#badge">
1818
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
1919
</a><a href="https://github.com/tannerlinsley/react-query/discussions">
2020
<img alt="Join the discussion on Github" src="https://img.shields.io/badge/Github%20Discussions%20%26%20Support-Chat%20now!-blue" />
21-
</a><a href="https://bestofjs.org/projects/react-query"><img alt="Best of JS" src="https://img.shields.io/endpoint?url=https://bestofjs-serverless.now.sh/api/project-badge?fullName=TanStack%2Fquery%26since=daily" /></a><a href="https://github.com/tannerlinsley/react-query" target="\_parent">
21+
</a><a href="https://bestofjs.org/projects/tanstack-query"><img alt="Best of JS" src="https://img.shields.io/endpoint?url=https://bestofjs-serverless.now.sh/api/project-badge?fullName=TanStack%2Fquery%26since=daily" /></a><a href="https://github.com/tannerlinsley/react-query" target="\_parent">
2222
<img alt="" src="https://img.shields.io/github/stars/tannerlinsley/react-query.svg?style=social&label=Star" />
2323
</a><a href="https://twitter.com/tannerlinsley" target="\_parent">
2424
<img alt="" src="https://img.shields.io/twitter/follow/tannerlinsley.svg?style=social&label=Follow" />
@@ -31,7 +31,7 @@ Enjoy this library? Try the entire [TanStack](https://tanstack.com)! [React Tabl
3131
## Visit [tanstack.com/query](https://tanstack.com/query) for docs, guides, API and more!
3232

3333
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/.
34+
Still on **React Query v3**? No problem! Check out the v3 docs here: https://react-query-v3.tanstack.com/.
3535

3636
## Quick Features
3737

@@ -45,8 +45,8 @@ Would you like to try **React Query v4beta**? Check out the v4 beta docs here: h
4545
- Request Cancellation
4646
- [React Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html) + Fetch-As-You-Render Query Prefetching
4747
- Dedicated Devtools
48-
- <a href="https://bundlephobia.com/result?p=react-query@latest" target="\_parent">
49-
<img alt="" src="https://badgen.net/bundlephobia/minzip/react-query@latest" />
48+
- <a href="https://bundlephobia.com/package/@tanstack/react-query@latest" target="\_parent">
49+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/react-query" />
5050
</a> (depending on features imported)
5151

5252
### [Become a Sponsor!](https://github.com/sponsors/tannerlinsley/)

docs/adapters/vue-query.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Solid Query (Coming Soon)
2+
title: Vue Query (Coming Soon)
33
---
44

55
> ⚠️ This module has not yet been developed. It requires an adapter similar to `react-query` to work. We estimate the amount of code to do this is low-to-moderate, but does require familiarity with the Vue framework. If you would like to contribute this adapter, please open a PR!
66
7-
The `@tanstack/vue-query` package offers a 1st-class API for using TanStack Query via Solid. However, all of the primitives you receive from this API are core APIs that are shared across all of the TanStack Adapters including the Query Client, query results, query subscriptions, etc.
7+
The `@tanstack/vue-query` package offers a 1st-class API for using TanStack Query via Vue. However, all of the primitives you receive from this API are core APIs that are shared across all of the TanStack Adapters including the Query Client, query results, query subscriptions, etc.

docs/config.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,10 @@
228228
"label": "Basic w/ GraphQL-Request",
229229
"to": "examples/react/basic-graphql-request"
230230
},
231-
{
232-
"label": "Custom Hooks",
233-
"to": "examples/react/custom-hooks"
234-
},
235231
{
236232
"label": "Auto Refetching / Polling / Realtime",
237233
"to": "examples/react/auto-refetching"
238234
},
239-
{
240-
"label": "Window Refocus Refetching",
241-
"to": "examples/react/focus-refetching"
242-
},
243-
{
244-
"label": "Optimistic Updates",
245-
"to": "examples/react/optimistic-updates"
246-
},
247235
{
248236
"label": "Optimistic Updates in TypeScript",
249237
"to": "examples/react/optimistic-updates-typescript"

docs/guides/important-defaults.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ If you see a refetch that you are not expecting, it is likely because you just f
3030
3131
- Query results by default are **structurally shared to detect if data has actually changed** and if not, **the data reference remains unchanged** to better help with value stabilization with regards to useMemo and useCallback. If this concept sounds foreign, then don't worry about it! 99.9% of the time you will not need to disable this and it makes your app more performant at zero cost to you.
3232

33-
> Structural sharing only works with JSON-compatible values, any other value types will always be considered as changed. If you are seeing performance issues because of large responses for example, you can disable this feature with the `config.structuralSharing` flag. If you are dealing with non-JSON compatible values in your query responses and still want to detect if data has changed or not, you can define a data compare function with `config.isDataEqual`.
33+
> Structural sharing only works with JSON-compatible values, any other value types will always be considered as changed. If you are seeing performance issues because of large responses for example, you can disable this feature with the `config.structuralSharing` flag. If you are dealing with non-JSON compatible values in your query responses and still want to detect if data has changed or not, you can define a data compare function with `config.isDataEqual` or provide your own custom function as `config.structuralSharing` to compute a value from the old and new responses, retaining references as required.
3434
3535
## Further Reading
3636

3737
Have a look at the following articles from our Community Resources for further explanations of the defaults:
3838

3939
- [Practical React Query](../community/tkdodos-blog#1-practical-react-query)
4040
- [React Query as a State Manager](../community/tkdodos-blog#10-react-query-as-a-state-manager)
41-

docs/guides/infinite-queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const { refetch } = useInfiniteQuery(['projects'], fetchProjects, {
108108
refetch({ refetchPage: (page, index) => index === 0 })
109109
```
110110

111-
You can also pass this function as part of the 2nd argument (`queryFilters`) to [queryClient.refetchQueries](/reference/QueryClient#queryclientrefetchqueries), [queryClient.invalidateQueries](/reference/QueryClient#queryclientinvalidatequeries) or [queryClient.resetQueries](/reference/QueryClient#queryclientresetqueries).
111+
You can also pass this function as part of the 2nd argument (`queryFilters`) to [queryClient.refetchQueries](../reference/QueryClient#queryclientrefetchqueries), [queryClient.invalidateQueries](../reference/QueryClient#queryclientinvalidatequeries) or [queryClient.resetQueries](../reference/QueryClient#queryclientresetqueries).
112112

113113
**Signature**
114114

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ React.useEffect(() => mySideEffectHere(data), [data])
252252

253253
### `persistQueryClient` and the corresponding persister plugins are no longer experimental and have been renamed
254254

255-
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.
255+
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.
256256

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

docs/guides/query-functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ The `QueryFunctionContext` is the object passed to each query function. It consi
6767

6868
- `queryKey: QueryKey`: [Query Keys](../guides/query-keys)
6969
- `pageParam: unknown | undefined`
70-
- only for [Infinite Queries](../guides/infinite-queries.md)
70+
- only for [Infinite Queries](../guides/infinite-queries)
7171
- the page parameter used to fetch the current page
7272
- signal?: AbortSignal
7373
- [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) instance provided by react-query
74-
- Can be used for [Query Cancellation](../guides/query-cancellation.md)
74+
- Can be used for [Query Cancellation](../guides/query-cancellation)
7575
- `meta?: Record<string, unknown>`
7676
- an optional field you can fill with additional information about your query
7777

docs/guides/testing.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Testing
55

66
React Query works by means of hooks - either the ones we offer or custom ones that wrap around them.
77

8-
Writing unit tests for these custom hooks can be done by means of the [React Hooks Testing Library](https://react-hooks-testing-library.com/) library.
8+
With React 17 or earlier, writing unit tests for these custom hooks can be done by means of the [React Hooks Testing Library](https://react-hooks-testing-library.com/) library.
99

1010
Install this by running:
1111

@@ -15,19 +15,21 @@ npm install @testing-library/react-hooks react-test-renderer --save-dev
1515

1616
(The `react-test-renderer` library is needed as a peer dependency of `@testing-library/react-hooks`, and needs to correspond to the version of React that you are using.)
1717

18+
*Note*: when using React 18 or later, `renderHook` is available directly through the `@testing-library/react` package, and `@testing-library/react-hooks` is no longer required.
19+
1820
## Our First Test
1921

2022
Once installed, a simple test can be written. Given the following custom hook:
2123

22-
```
24+
```ts
2325
export function useCustomHook() {
2426
return useQuery(['customHook'], () => 'Hello');
2527
}
2628
```
2729

28-
We can write a test for this as follows:
30+
Using React 17 or earlier, we can write a test for this as follows:
2931

30-
```
32+
```tsx
3133
const queryClient = new QueryClient();
3234
const wrapper = ({ children }) => (
3335
<QueryClientProvider client={queryClient}>
@@ -42,6 +44,18 @@ await waitFor(() => result.current.isSuccess);
4244
expect(result.current.data).toEqual("Hello");
4345
```
4446

47+
Using React 18 or later, the semantics of `waitFor` have changed, and the above test needs to be modified as follows:
48+
49+
```tsx
50+
import { renderHook, waitFor } from "@testing-library/react";
51+
52+
...
53+
54+
const { result } = renderHook(() => useCustomHook(), { wrapper });
55+
56+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
57+
```
58+
4559
Note that we provide a custom wrapper that builds the `QueryClient` and `QueryClientProvider`. This helps to ensure that our test is completely isolated from any other tests.
4660

4761
It is possible to write this wrapper only once, but if so we need to ensure that the `QueryClient` gets cleared before every test, and that tests don't run in parallel otherwise one test will influence the results of others.
@@ -50,7 +64,7 @@ It is possible to write this wrapper only once, but if so we need to ensure that
5064

5165
The library defaults to three retries with exponential backoff, which means that your tests are likely to timeout if you want to test an erroneous query. The easiest way to turn retries off is via the QueryClientProvider. Let's extend the above example:
5266

53-
```
67+
```tsx
5468
const queryClient = new QueryClient({
5569
defaultOptions: {
5670
queries: {
@@ -98,15 +112,15 @@ There are plenty of ways that these can be tested, but for this example we are g
98112

99113
Given the following custom hook:
100114

101-
```
115+
```ts
102116
function useFetchData() {
103117
return useQuery(['fetchData'], () => request('/api/data'));
104118
}
105119
```
106120

107121
We can write a test for this as follows:
108122

109-
```
123+
```tsx
110124
const queryClient = new QueryClient();
111125
const wrapper = ({ children }) => (
112126
<QueryClientProvider client={queryClient}>
@@ -129,13 +143,13 @@ await waitFor(() => {
129143
expect(result.current.data).toEqual({answer: 42});
130144
```
131145

132-
Here we are making use of `waitFor` and waiting until the query status indicates that the request has succeeded. This way we know that our hook has finished and should have the correct data.
146+
Here we are making use of `waitFor` and waiting until the query status indicates that the request has succeeded. This way we know that our hook has finished and should have the correct data. *Note*: when using React 18, the semantics of `waitFor` have changed as noted above.
133147

134148
## Testing Load More / Infinite Scroll
135149

136150
First we need to mock our API response
137151

138-
```
152+
```ts
139153
function generateMockedResponse(page) {
140154
return {
141155
page: page,
@@ -147,7 +161,7 @@ function generateMockedResponse(page) {
147161
Then, our `nock` configuration needs to differentiate responses based on the page, and we'll be using `uri` to do this.
148162
`uri`'s value here will be something like `"/?page=1` or `/?page=2`
149163

150-
```
164+
```ts
151165
const expectation = nock('http://example.com')
152166
.persist()
153167
.query(true)
@@ -163,7 +177,7 @@ const expectation = nock('http://example.com')
163177

164178
Now we can safely run our tests, the trick here is to await both `isFetching` and then `!isFetching` after calling `fetchNextPage()`:
165179

166-
```
180+
```ts
167181
const { result, waitFor } = renderHook(() => useInfiniteQueryCustomHook(), { wrapper });
168182

169183
await waitFor(() => result.current.isSuccess);
@@ -183,6 +197,8 @@ expect(result.current.data.pages).toStrictEqual([
183197
expectation.done();
184198
```
185199

200+
*Note*: when using React 18, the semantics of `waitFor` have changed as noted above.
201+
186202
## Further reading
187203

188204
For additional tips and an alternative setup using `mock-service-worker`, have a look at [Testing React Query](../community/tkdodos-blog#5-testing-react-query) from

docs/plugins/persistQueryClient.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ This is set of utilities for interacting with "persisters" which save your query
77

88
## Build Persisters
99

10-
- [createSyncStoragePersister](/plugins/createSyncStoragePersister)
11-
- [createAsyncStoragePersister](/plugins/createAsyncStoragePersister)
10+
- [createSyncStoragePersister](../plugins/createSyncStoragePersister)
11+
- [createAsyncStoragePersister](../plugins/createAsyncStoragePersister)
1212
- [create a custom persister](#persisters)
1313

1414
## How It Works
@@ -211,7 +211,7 @@ ReactDOM.createRoot(rootElement).render(
211211
`PersistQueryClientProvider` takes the same props as [QueryClientProvider](../reference/QueryClientProvider), and additionally:
212212

213213
- `persistOptions: PersistQueryClientOptions`
214-
- all [options](#options) you cann pass to [persistQueryClient](#persistqueryclient) minus the QueryClient itself
214+
- all [options](#options) you can pass to [persistQueryClient](#persistqueryclient) minus the QueryClient itself
215215
- `onSuccess?: () => void`
216216
- optional
217217
- will be called when the initial restore is finished

docs/quick-start.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ id: quick-start
33
title: Quick Start
44
---
55

6-
This example very briefly illustrates the 3 core concepts of React Query:
6+
This code snippet very briefly illustrates the 3 core concepts of React Query:
77

88
- [Queries](./guides/queries)
99
- [Mutations](./guides/mutations)
1010
- [Query Invalidation](./guides/query-invalidation)
1111

12+
If you're looking for a fully functioning example, please have a look at our [simple codesandbox example](../examples/react/simple)
13+
14+
1215
```tsx
1316
import {
1417
useQuery,
@@ -49,7 +52,7 @@ function Todos() {
4952
return (
5053
<div>
5154
<ul>
52-
{query.data.map(todo => (
55+
{query.data?.map(todo => (
5356
<li key={todo.id}>{todo.title}</li>
5457
))}
5558
</ul>

0 commit comments

Comments
 (0)