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 shouldDehydrateMutation,
16})
17```
18β
19**Options**
20β
21- `client: QueryClient`
22 - **Required**
23 - The `queryClient` that should be dehydrated
24- `options: DehydrateOptions`
25 - Optional
26 - `shouldDehydrateMutation: (mutation: Mutation) => boolean`
27 - Optional
28 - Whether to dehydrate mutations.
29 - The function is called for each mutation in the cache
30 - Return `true` to include this mutation in dehydration, or `false` otherwise
31 - Defaults to only including paused mutations
32 - If you would like to extend the function while retaining the default behavior, import and execute `defaultShouldDehydrateMutation` as part of the return statement
33 - `shouldDehydrateQuery: (query: Query) => boolean`
34 - Optional
35 - Whether to dehydrate queries.
36 - The function, it is called for each query in the cache
37 - Return `true` to include this query in dehydration, or `false` otherwise
38 - Defaults to only including successful queries
39 - If you would like to extend the function while retaining the default behavior, import and execute `defaultShouldDehydrateQuery` as part of the return statement
40 - `serializeData?: (data: any) => any` A function to transform (serialize) data during dehydration.
41β
42**Returns**
43β
44- `dehydratedState: DehydratedState`
45 - This includes everything that is needed to hydrate the `queryClient` at a later point
46 - 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
47 - This result is not in serialized form, you need to do that yourself if desired
48β
49### Limitations
50β
51Some 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.:
52β
53```tsx
54// server
55const state = dehydrate(client, { shouldDehydrateQuery: () => true }) // to also include Errors
56const serializedState = mySerialize(state) // transform Error instances to objects
57β
58// client
59const state = myDeserialize(serializedState) // transform objects back to Error instances
60hydrate(client, state)
61```
62β
63## `hydrate`
64β
65`hydrate` adds a previously dehydrated state into a `cache`.
66β
67```tsx
68import { hydrate } from '@tanstack/react-query'
69β
70hydrate(queryClient, dehydratedState, options)
71```
72β
73**Options**
74β
75- `client: QueryClient`
76 - **Required**
77 - The `queryClient` to hydrate the state into
78- `dehydratedState: DehydratedState`
79 - **Required**
80 - The state to hydrate into the client
81- `options: HydrateOptions`
82 - Optional
83 - `defaultOptions: DefaultOptions`
84 - Optional
85 - `mutations: MutationOptions` The default mutation options to use for the hydrated mutations.
86 - `queries: QueryOptions` The default query options to use for the hydrated queries.
87 - `deserializeData?: (data: any) => any` A function to transform (deserialize) data before it is put into the cache.
88 - `queryClient?: QueryClient`,
89 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
90β
91### Limitations
92β
93If the queries you're trying to hydrate already exist in the queryCache, `hydrate` will only overwrite them if the data is newer than the data present in the cache. Otherwise, it will **not** get applied.
94β
95[//]: # 'HydrationBoundary'
96β
97## `HydrationBoundary`
98β
99`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.
100β
101```tsx
102import { HydrationBoundary } from '@tanstack/react-query'
103β
104function App() {
105 return <HydrationBoundary state={dehydratedState}>...</HydrationBoundary>
106}
107```
108β
109> Note: Only `queries` can be dehydrated with an `HydrationBoundary`.
110β
111**Options**
112β
113- `state: DehydratedState`
114 - The state to hydrate
115- `options: HydrateOptions`
116 - Optional
117 - `defaultOptions: QueryOptions`
118 - The default query options to use for the hydrated queries.
119 - `queryClient?: QueryClient`,
120 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
121β
122[//]: # 'HydrationBoundary'