Skip to content

Commit 710e724

Browse files
committed
Merge branch 'master' into feature/2682-error-query-doesn't-fire-again-when-keys-change
2 parents 6f7fa29 + 1896ca2 commit 710e724

File tree

20 files changed

+275
-227
lines changed

20 files changed

+275
-227
lines changed

docs/src/manifests/manifest.json

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -435,26 +435,11 @@
435435
"editUrl": "/reference/setLogger.md"
436436
},
437437
{
438-
"title": "hydration/Hydrate",
439-
"path": "/reference/hydration/HydrateComp",
440-
"editUrl": "/reference/hydration/HydrateComp.md"
441-
},
442-
{
443-
"title": "hydration/useHydrate",
444-
"path": "/reference/hydration/useHydrate",
445-
"editUrl": "/reference/hydration/useHydrate.md"
446-
},
447-
{
448-
"title": "hydration/hydrate",
449-
"path": "/reference/hydration/hydrate",
450-
"editUrl": "/reference/hydration/hydrate.md"
451-
},
452-
{
453-
"title": "hydration/dehydrate",
454-
"path": "/reference/hydration/dehydrate",
455-
"editUrl": "/reference/hydration/dehydrate.md"
438+
"title": "hydration",
439+
"path": "/reference/hydration",
440+
"editUrl": "/reference/hydration.md"
456441
}
457442
]
458443
}
459444
]
460-
}
445+
}

docs/src/pages/community/tkdodos-blog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ React Query maintainer [TkDodo](https://twitter.com/tkdodo) has a series of blog
4545
## [#10: React Query as a State Manager](https://tkdodo.eu/blog/react-query-as-a-state-manager)
4646

4747
> React Query doesn't fetch any data for you - it's a data synchronization tool that excels when used for server state. This article has everything you need to know to make React Query your single source of truth state manager for your async state. You'll learn how to let React Query do it's magic and why customizing `staleTime` might be all you need. [Read more...](https://tkdodo.eu/blog/react-query-as-a-state-manager)
48+
49+
## [#11: React Query Error Handling](https://tkdodo.eu/blog/react-query-error-handling)
50+
51+
> Handling errors is an integral part of working with asynchronous data, especially data fetching. We have to face it: Not all requests will be successful, and not all Promises will be fulfilled. This blog post describes various ways of coping with errors in React Query, such as the error property, using Error Boundaries or onError callbacks, so that you can prepare your application for the cases when "Something went wrong". [Read more...](https://tkdodo.eu/blog/react-query-error-handling)

docs/src/pages/guides/ssr.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ To support caching queries on the server and set up hydration:
5555

5656
```js
5757
// _app.jsx
58-
import { QueryClient, QueryClientProvider } from 'react-query'
59-
import { Hydrate } from 'react-query/hydration'
58+
import { Hydrate, QueryClient, QueryClientProvider } from 'react-query'
6059

6160
export default function MyApp({ Component, pageProps }) {
6261
const [queryClient] = React.useState(() => new QueryClient())
@@ -79,8 +78,7 @@ Now you are ready to prefetch some data in your pages with either [`getStaticPro
7978

8079
```js
8180
// pages/posts.jsx
82-
import { QueryClient, useQuery } from 'react-query'
83-
import { dehydrate } from 'react-query/hydration'
81+
import { dehydrate, QueryClient, useQuery } from 'react-query';
8482

8583
export async function getStaticProps() {
8684
const queryClient = new QueryClient()
@@ -126,8 +124,7 @@ This guide is at-best, a high level overview of how SSR with React Query should
126124
> SECURITY NOTE: Serializing data with `JSON.stringify` can put you at risk for XSS-vulnerabilities, [this blog post explains why and how to solve it](https://medium.com/node-security/the-most-common-xss-vulnerability-in-react-js-applications-2bdffbcc1fa0)
127125
128126
```js
129-
import { QueryClient, QueryClientProvider } from 'react-query'
130-
import { dehydrate, Hydrate } from 'react-query/hydration'
127+
import { dehydrate, Hydrate, QueryClient, QueryClientProvider } from 'react-query';
131128

132129
function handleRequest (req, res) {
133130
const queryClient = new QueryClient()
@@ -162,8 +159,7 @@ function handleRequest (req, res) {
162159
- Render your app with the client provider and also **using the dehydrated state. This is extremely important! You must render both server and client using the same dehydrated state to ensure hydration on the client produces the exact same markup as the server.**
163160

164161
```js
165-
import { QueryClient, QueryClientProvider } from 'react-query'
166-
import { Hydrate } from 'react-query/hydration'
162+
import { Hydrate, QueryClient, QueryClientProvider } from 'react-query'
167163

168164
const dehydratedState = window.__REACT_QUERY_STATE__
169165

docs/src/pages/guides/testing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ await waitFor(() => {
121121
return result.current.isSuccess;
122122
});
123123
124-
expect(result.current).toEqual({answer: 42});
124+
expect(result.current.data).toEqual({answer: 42});
125125
```
126126

127127
Here we are making use of `waitFor` and waiting until our the query status indicates that the request has succeeded. This way we know that our hook has finished and should have the correct data.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
id: hydration
3+
title: hydration
4+
---
5+
6+
## `dehydrate`
7+
8+
`dehydrate` creates a frozen representation of a `cache` that can later be hydrated with `Hydrate`, `useHydrate`, or `hydrate`. This is useful for passing prefetched queries from server to client or persisting queries to localstorage or other persistent locations. It only includes currently successful queries by default.
9+
10+
```js
11+
import { dehydrate } from 'react-query'
12+
13+
const dehydratedState = dehydrate(queryClient, {
14+
shouldDehydrateQuery,
15+
})
16+
```
17+
18+
> Note: Since version `3.22.0` hydration utilities moved into to core. If you using lower version your should import `dehydrate` from `react-query/hydration`
19+
20+
**Options**
21+
22+
- `client: QueryClient`
23+
- **Required**
24+
- The `queryClient` that should be dehydrated
25+
- `options: DehydrateOptions`
26+
- Optional
27+
- `dehydrateMutations: boolean`
28+
- Optional
29+
- Whether or not to dehydrate mutations.
30+
- `dehydrateQueries: boolean`
31+
- Optional
32+
- Whether or not to dehydrate queries.
33+
- `shouldDehydrateMutation: (mutation: Mutation) => boolean`
34+
- Optional
35+
- This function is called for each mutation in the cache
36+
- Return `true` to include this mutation in dehydration, or `false` otherwise
37+
- The default version only includes paused mutations
38+
- `shouldDehydrateQuery: (query: Query) => boolean`
39+
- Optional
40+
- This function is called for each query in the cache
41+
- Return `true` to include this query in dehydration, or `false` otherwise
42+
- The default version only includes successful queries, do `shouldDehydrateQuery: () => true` to include all queries
43+
44+
**Returns**
45+
46+
- `dehydratedState: DehydratedState`
47+
- This includes everything that is needed to hydrate the `queryClient` at a later point
48+
- You **should not** rely on the exact format of this response, it is not part of the public API and can change at any time
49+
- This result is not in serialized form, you need to do that yourself if desired
50+
51+
## `hydrate`
52+
53+
`hydrate` adds a previously dehydrated state into a `cache`. If the queries included in dehydration already exist in the queryCache, `hydrate` does not overwrite them.
54+
55+
```js
56+
import { hydrate } from 'react-query'
57+
58+
hydrate(queryClient, dehydratedState, options)
59+
```
60+
61+
> Note: Since version `3.22.0` hydration utilities moved into to core. If you using lower version your should import `hydrate` from `react-query/hydration`
62+
63+
**Options**
64+
65+
- `client: QueryClient`
66+
- **Required**
67+
- The `queryClient` to hydrate the state into
68+
- `dehydratedState: DehydratedState`
69+
- **Required**
70+
- The state to hydrate into the client
71+
- `options: HydrateOptions`
72+
- Optional
73+
- `defaultOptions: DefaultOptions`
74+
- Optional
75+
- `mutations: MutationOptions` The default mutation options to use for the hydrated mutations.
76+
- `queries: QueryOptions` The default query options to use for the hydrated queries.
77+
78+
## `useHydrate`
79+
80+
`useHydrate` adds a previously dehydrated state into the `queryClient` that would be returned by `useQueryClient()`. If the client already contains data, the new queries will be intelligently merged based on update timestamp.
81+
82+
```jsx
83+
import { useHydrate } from 'react-query'
84+
85+
useHydrate(dehydratedState, options)
86+
```
87+
88+
> Note: Since version `3.22.0` hydration utilities moved into to core. If you using lower version your should import `useHydrate` from `react-query/hydration`
89+
90+
**Options**
91+
92+
- `dehydratedState: DehydratedState`
93+
- **Required**
94+
- The state to hydrate
95+
- `options: HydrateOptions`
96+
- Optional
97+
- `defaultOptions: QueryOptions`
98+
- The default query options to use for the hydrated queries.
99+
100+
## `Hydrate`
101+
102+
`Hydrate` wraps `useHydrate` into component. Can be useful when you need hydrate in class component or need hydrate on same level where `QueryClientProvider` rendered.
103+
104+
```js
105+
import { Hydrate } from 'react-query'
106+
107+
function App() {
108+
return <Hydrate state={dehydratedState}>...</Hydrate>
109+
}
110+
```
111+
112+
> Note: Since version `3.22.0` hydration utilities moved into to core. If you using lower version your should import `Hydrate` from `react-query/hydration`
113+
114+
**Options**
115+
116+
- `state: DehydratedState`
117+
- The state to hydrate
118+
- `options: HydrateOptions`
119+
- Optional
120+
- `defaultOptions: QueryOptions`
121+
- The default query options to use for the hydrated queries.

docs/src/pages/reference/hydration/HydrateComp.md

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

docs/src/pages/reference/hydration/dehydrate.md

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

docs/src/pages/reference/hydration/hydrate.md

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

docs/src/pages/reference/hydration/useHydrate.md

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

docs/src/pages/reference/useQuery.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
isPlaceholderData,
2121
isPreviousData,
2222
isRefetchError,
23+
isRefetching,
2324
isStale,
2425
isSuccess,
2526
refetch,
@@ -220,6 +221,9 @@ const result = useQuery({
220221
- `isFetching: boolean`
221222
- Is `true` whenever a request is in-flight, which includes initial `loading` as well as background refetches.
222223
- Will be `true` if the query is currently fetching, including background fetching.
224+
- `isRefetching: boolean`
225+
- Is `true` whenever a background refetch is in-flight, which _does not_ include initial `loading`
226+
- Is the same as `isFetching && !isLoading`
223227
- `failureCount: number`
224228
- The failure count for the query.
225229
- Incremented every time the query fails.

0 commit comments

Comments
 (0)