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β
41**Returns**
42β
43- `dehydratedState: DehydratedState`
44 - This includes everything that is needed to hydrate the `queryClient` at a later point
45 - 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
46 - This result is not in serialized form, you need to do that yourself if desired
47β
48### limitations
49β
50Some 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.:
51β
52```tsx
53// server
54const state = dehydrate(client, { shouldDehydrateQuery: () => true }) // to also include Errors
55const serializedState = mySerialize(state) // transform Error instances to objects
56β
57// client
58const state = myDeserialize(serializedState) // transform objects back to Error instances
59hydrate(client, state)
60```
61β
62## `hydrate`
63β
64`hydrate` adds a previously dehydrated state into a `cache`.
65β
66```tsx
67import { hydrate } from '@tanstack/react-query'
68β
69hydrate(queryClient, dehydratedState, options)
70```
71β
72**Options**
73β
74- `client: QueryClient`
75 - **Required**
76 - The `queryClient` to hydrate the state into
77- `dehydratedState: DehydratedState`
78 - **Required**
79 - The state to hydrate into the client
80- `options: HydrateOptions`
81 - Optional
82 - `defaultOptions: DefaultOptions`
83 - Optional
84 - `mutations: MutationOptions` The default mutation options to use for the hydrated mutations.
85 - `queries: QueryOptions` The default query options to use for the hydrated queries.
86 - `queryClient?: QueryClient`,
87 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
88β
89### Limitations
90β
91If the queries included in dehydration already exist in the queryCache, `hydrate` does not overwrite them and they will be **silently** discarded.
92β
93[//]: # 'HydrationBoundary'
94β
95## `HydrationBoundary`
96β
97`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.
98β
99```tsx
100import { HydrationBoundary } from '@tanstack/react-query'
101β
102function App() {
103 return <HydrationBoundary state={dehydratedState}>...</HydrationBoundary>
104}
105```
106β
107**Options**
108β
109- `state: DehydratedState`
110 - The state to hydrate
111- `options: HydrateOptions`
112 - Optional
113 - `defaultOptions: QueryOptions`
114 - The default query options to use for the hydrated queries.
115 - `queryClient?: QueryClient`,
116 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
117β
118[//]: # 'HydrationBoundary'