You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/pages/docs/api.md
+8-20Lines changed: 8 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -792,13 +792,13 @@ setConsole({
792
792
793
793
## `hydration/dehydrate`
794
794
795
-
`dehydrate` creates a frozen representation of a `queryCache` that can later be hydrated with `useHydrate`, `hydrate` or by passing it into `hydration/ReactQueryCacheProvider`. This is useful for passing prefetched queries from server to client or persisting queries to localstorage. It only includes currently successful queries by default.
795
+
`dehydrate` creates a frozen representation of a `queryCache` that can later be hydrated with `useHydrate`, `hydrate` or `Hydrate`. This is useful for passing prefetched queries from server to client or persisting queries to localstorage. It only includes currently successful queries by default.
`hydrate` adds a previously dehydrated state into a `queryCache`. If the queries included in dehydration already exist in the cache, `hydrate` does not overwrite them.
825
825
826
826
```js
827
-
import {hydrate } from'react-query/hydration'
827
+
import {hydrate } from'react-query/hydration'
828
828
829
829
hydrate(queryCache, dehydratedState)
830
830
```
@@ -854,31 +854,19 @@ useHydrate(dehydratedState)
854
854
- **Required**
855
855
- The state to hydrate
856
856
857
-
## `hydration/ReactQueryCacheProvider`
857
+
## `hydration/Hydrate`
858
858
859
-
`hydration/ReactQueryCacheProvider` does the same thing as `ReactQueryCacheProvider` but also supports hydrating an initial state into the cache.
859
+
`hydration/Hydrate` does the same thing as `useHydrate` but exposed as a component.
Copy file name to clipboardExpand all lines: docs/src/pages/docs/guides/ssr.md
+30-23Lines changed: 30 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,11 +13,11 @@ This approach works well for applications or user-specific pages that might cont
13
13
14
14
React Query supports two ways of prefetching data on the server and passing that to the client.
15
15
16
-
* Prefetch the data yourself and pass it in as `initialData`
17
-
* Quick to set up for simple cases
18
-
* Has some caveats
19
-
* Prefetch the query via React Query and use de/rehydration
20
-
* Requires slightly more setup up front
16
+
- Prefetch the data yourself and pass it in as `initialData`
17
+
- Quick to set up for simple cases
18
+
- Has some caveats
19
+
- Prefetch the query via React Query and use de/rehydration
20
+
- Requires slightly more setup up front
21
21
22
22
The exact implementation of these mechanisms may vary from platform to platform, but we recommend starting with Next.js which supports [2 forms of pre-rendering](https://nextjs.org/docs/basic-features/data-fetching):
23
23
@@ -45,28 +45,29 @@ function Posts(props) {
45
45
46
46
The setup is minimal and this can be a perfect solution for some cases, but there are a few tradeoffs compared to the full approach:
47
47
48
-
* If you are calling `useQuery` in a component deeper down in the tree you need to pass the `initialData` down to that point
49
-
* If you are calling `useQuery` with the same query in multiple locations, you need to pass `initialData` to all of them
50
-
* There is no way to know at what time the query was fetched on the server, so `updatedAt` and determining if the query needs refetching is based on when the page loaded instead
48
+
- If you are calling `useQuery` in a component deeper down in the tree you need to pass the `initialData` down to that point
49
+
- If you are calling `useQuery` with the same query in multiple locations, you need to pass `initialData` to all of them
50
+
- There is no way to know at what time the query was fetched on the server, so `updatedAt` and determining if the query needs refetching is based on when the page loaded instead
51
51
52
52
## Prefetch the query via React Query and use de/rehydration
53
53
54
54
React Query supports prefetching a query on the server and handing off or _dehydrating_ that query to the client. This means the server can prerender markup that is immediately available on page load and as soon as JS is available, React Query can upgrade or _hydrate_ those queries with the full functionality of the library. This includes refetching those queries on the client if they have become stale since the time they were rendered on the server.
55
55
56
56
### Integrating with Next.js
57
57
58
-
To support caching queries on the server and set up hydration, you start with wrapping your application with `<ReactQueryCacheProvider>` in `_app.js`.
59
-
60
-
> Note: You need to import `ReactQueryCacheProvider` from `'react-query/hydration'` for it to support hydration!
58
+
To support caching queries on the server and set up hydration, you start with wrapping your application with `<ReactQueryCacheProvider>` and `<Hydrate>` in `_app.js`.
@@ -86,8 +87,8 @@ export async function getStaticProps() {
86
87
87
88
return {
88
89
props: {
89
-
dehydratedState:dehydrate(queryCache)
90
-
}
90
+
dehydratedState:dehydrate(queryCache),
91
+
},
91
92
}
92
93
}
93
94
@@ -119,7 +120,7 @@ Since there are many different possible setups for SSR, it's hard to give a deta
119
120
- Call `prefetchQueryCache.prefetchQuery(...)` to prefetch queries
120
121
- Dehydrate by using `const dehydratedState = dehydrate(prefetchQueryCache)`
121
122
- Render
122
-
- Wrap the app in `<ReactQueryCacheProvider>`from `'react-query/hydration'`and pass in `dehydratedState`
123
+
- Wrap the app in `<ReactQueryCacheProvider>`and `<Hydrate>` to create a new cache and hydrate the state
123
124
- This makes sure a separate `queryCache` is created specifically for rendering
124
125
-**Do not** pass in the `prefetchQueryCache` from the last step, the server and client both needs to render from the dehydrated data to avoid React hydration mismatches. This is because queries with errors are excluded from dehydration by default.
125
126
- Serialize and embed `dehydratedState` in the markup
@@ -129,7 +130,7 @@ Since there are many different possible setups for SSR, it's hard to give a deta
129
130
130
131
- Parse `dehydratedState` from where you put it in the markup
131
132
- Render
132
-
- Wrap the app in `<ReactQueryCacheProvider>` from `'react-query/hydration'` and pass in `dehydratedState`
133
+
- Wrap the app in `<Hydrate>` from `'react-query/hydration'` and pass in the dehyrated state.
133
134
134
135
This list aims to be exhaustive, but depending on your current setup, the above steps can take more or less work. Here is a barebones example:
0 commit comments