Open History to see the last commit
1---
2id: hydration
3title: hydration
4---
5β
6## `dehydrate`
7β
8`dehydrate` creates a frozen representation of a `cache` that can later be hydrated with `HydrationBoundary` 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```tsx
11import { dehydrate } from '@tanstack/react-query'
12β
13const dehydratedState = dehydrate(queryClient, {
14 shouldDehydrateQuery,
15})
16```
17β
18**Options**
19β
20- `client: QueryClient`
21 - **Required**
22 - The `queryClient` that should be dehydrated
23- `options: DehydrateOptions`
24 - Optional
25 - `dehydrateMutations: boolean`
26 - Optional
27 - Whether or not to dehydrate mutations.
28 - `dehydrateQueries: boolean`
29 - Optional
30 - Whether or not to dehydrate queries.
31 - `shouldDehydrateMutation: (mutation: Mutation) => boolean`
32 - Optional
33 - This function is called for each mutation in the cache
34 - Return `true` to include this mutation in dehydration, or `false` otherwise
35 - The default version only includes paused mutations
36 - If you would like to extend the function while retaining the previous behavior, import and execute `defaultShouldDehydrateMutation` as part of the return statement
37 - `shouldDehydrateQuery: (query: Query) => boolean`
38 - Optional
39 - This function is called for each query in the cache
40 - Return `true` to include this query in dehydration, or `false` otherwise
41 - The default version only includes successful queries, do `shouldDehydrateQuery: () => true` to include all queries
42 - If you would like to extend the function while retaining the previous behavior, import and execute `defaultShouldDehydrateQuery` as part of the return statement
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### limitations
52β
53Some storage systems (such as browser [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)) require values to be JSON serializable. If you need to dehydrate values that are not automatically serializable to JSON (like `Error` or `undefined`), you have to serialize them for yourself. Since only successful queries are included per default, to also include `Errors`, you have to provide `shouldDehydrateQuery`, e.g.:
54β
55```tsx
56// server
57const state = dehydrate(client, { shouldDehydrateQuery: () => true }) // to also include Errors
58const serializedState = mySerialize(state) // transform Error instances to objects
59β
60// client
61const state = myDeserialize(serializedState) // transform objects back to Error instances
62hydrate(client, state)
63```
64β
65## `hydrate`
66β
67`hydrate` adds a previously dehydrated state into a `cache`.
68β
69```tsx
70import { hydrate } from '@tanstack/react-query'
71β
72hydrate(queryClient, dehydratedState, options)
73```
74β
75**Options**
76β
77- `client: QueryClient`
78 - **Required**
79 - The `queryClient` to hydrate the state into
80- `dehydratedState: DehydratedState`
81 - **Required**
82 - The state to hydrate into the client
83- `options: HydrateOptions`
84 - Optional
85 - `defaultOptions: DefaultOptions`
86 - Optional
87 - `mutations: MutationOptions` The default mutation options to use for the hydrated mutations.
88 - `queries: QueryOptions` The default query options to use for the hydrated queries.
89 - `queryClient?: QueryClient`,
90 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
91β
92### Limitations
93β
94If the queries included in dehydration already exist in the queryCache, `hydrate` does not overwrite them and they will be **silently** discarded.
95β
96β
97[//]: # 'HydrationBoundary'
98## `HydrationBoundary`
99β
100`HydrationBoundary` 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.
101β
102```tsx
103import { HydrationBoundary } from '@tanstack/react-query'
104β
105function App() {
106 return <HydrationBoundary state={dehydratedState}>...</HydrationBoundary>
107}
108```
109β
110**Options**
111β
112- `state: DehydratedState`
113 - The state to hydrate
114- `options: HydrateOptions`
115 - Optional
116 - `defaultOptions: QueryOptions`
117 - The default query options to use for the hydrated queries.
118 - `queryClient?: QueryClient`,
119 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
120β
121[//]: # 'HydrationBoundary'