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 - `shouldDehydrateQuery: (query: Query) => boolean`
37 - Optional
38 - This function is called for each query in the cache
39 - Return `true` to include this query in dehydration, or `false` otherwise
40 - The default version only includes successful queries, do `shouldDehydrateQuery: () => true` to include all queries
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 - `context?: React.Context<QueryClient | undefined>`
88 - Use this to use a custom React Query context. Otherwise, `defaultContext` will be used.
89β
90### Limitations
91β
92If the queries included in dehydration already exist in the queryCache, `hydrate` does not overwrite them and they will be **silently** discarded.
93β
94## `useHydrate`
95β
96`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.
97β
98```tsx
99import { useHydrate } from '@tanstack/react-query'
100β
101useHydrate(dehydratedState, options)
102```
103β
104**Options**
105β
106- `dehydratedState: DehydratedState`
107 - **Required**
108 - The state to hydrate
109- `options: HydrateOptions`
110 - Optional
111 - `defaultOptions: QueryOptions`
112 - The default query options to use for the hydrated queries.
113 - `context?: React.Context<QueryClient | undefined>`
114 - Use this to use a custom React Query context. Otherwise, `defaultContext` will be used.
115β
116## `Hydrate`
117β
118`Hydrate` wraps `useHydrate` into component. Can be useful when you need hydrate in class component or need hydrate on same level where `QueryClientProvider` rendered.
119β
120```tsx
121import { Hydrate } from '@tanstack/react-query'
122β
123function App() {
124 return <Hydrate state={dehydratedState}>...</Hydrate>
125}
126```
127β
128**Options**
129β
130- `state: DehydratedState`
131 - The state to hydrate
132- `options: HydrateOptions`
133 - Optional
134 - `defaultOptions: QueryOptions`
135 - The default query options to use for the hydrated queries.
136 - `context?: React.Context<QueryClient | undefined>`
137 - Use this to use a custom React Query context. Otherwise, `defaultContext` will be used.