Skip to content

Commit b88cda4

Browse files
docs: use hydration utilities from core (TanStack#2643)
1 parent 058977a commit b88cda4

File tree

9 files changed

+131
-148
lines changed

9 files changed

+131
-148
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/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

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.

examples/nextjs/pages/_app.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react'
2-
import { QueryClient, QueryClientProvider } from 'react-query'
3-
import { Hydrate } from 'react-query/hydration'
2+
import { Hydrate, QueryClient, QueryClientProvider } from 'react-query'
43
import { ReactQueryDevtools } from 'react-query/devtools'
54

65
export default function MyApp({ Component, pageProps }) {

examples/nextjs/pages/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react'
2-
import { QueryClient } from 'react-query'
3-
import { dehydrate } from 'react-query/hydration'
2+
import { dehydrate, QueryClient } from 'react-query'
43
import { Layout, Header, InfoBox, PostList } from '../components'
54
import { fetchPosts } from '../hooks'
65

0 commit comments

Comments
 (0)