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 `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```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 - `context?: React.Context<QueryClient | undefined>`
90 - Use this to use a custom React Query context. Otherwise, `defaultContext` 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[//]: # 'useHydrate'
97β
98## `useHydrate`
99β
100`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.
101β
102```tsx
103import { useHydrate } from '@tanstack/react-query'
104β
105useHydrate(dehydratedState, options)
106```
107β
108**Options**
109β
110- `dehydratedState: DehydratedState`
111 - **Required**
112 - The state to hydrate
113- `options: HydrateOptions`
114 - Optional
115 - `defaultOptions: QueryOptions`
116 - The default query options to use for the hydrated queries.
117 - `context?: React.Context<QueryClient | undefined>`
118 - Use this to use a custom React Query context. Otherwise, `defaultContext` will be used.
119β
120[//]: # 'useHydrate'
121[//]: # 'Hydrate'
122β
123## `Hydrate`
124β
125`Hydrate` wraps `useHydrate` into component. Can be useful when you need hydrate in class component or need hydrate on same level where `QueryClientProvider` rendered.
126β
127```tsx
128import { Hydrate } from '@tanstack/react-query'
129β
130function App() {
131 return <Hydrate state={dehydratedState}>...</Hydrate>
132}
133```
134β
135**Options**
136β
137- `state: DehydratedState`
138 - The state to hydrate
139- `options: HydrateOptions`
140 - Optional
141 - `defaultOptions: QueryOptions`
142 - The default query options to use for the hydrated queries.
143 - `context?: React.Context<QueryClient | undefined>`
144 - Use this to use a custom React Query context. Otherwise, `defaultContext` will be used.
145β
146[//]: # 'Hydrate'