Open History to see the last commit
1---
2id: hydrate
3title: hydrate
4---
5β
6```ts
7function hydrate(
8 client: QueryClient,
9 dehydratedState: Partial<DehydratedState>,
10 options?: HydrateOptions): void;
11```
12β
13Defined in: [packages/query-core/src/hydration.ts:306](https://github.com/TanStack/query/blob/main/packages/query-core/src/hydration.ts#L306)
14β
15Restores a `DehydratedState` (as produced by `dehydrate`) into a `QueryClient`'s cache, typically to seed the
16client with data already fetched on the server. `mutations` and `queries` are each optional on `dehydratedState`.
17Queries not yet in the cache are built from the dehydrated snapshot; queries that already exist are only updated
18when the dehydrated data is newer than what's already cached. Newly built queries have their `fetchStatus` reset
19to `'idle'` so they don't hydrate stuck in a fetching state. If a dehydrated query still had an in-flight
20promise, it is resumed via `query.fetch()` (reusing that promise as `initialPromise`) rather than re-invoking
21`queryFn`.
22β
23## Parameters
24β
25### client
26β
27[`QueryClient`](../classes/QueryClient.md)
28β
29The client whose cache is restored into.
30β
31### dehydratedState
32β
33`Partial`\<[`DehydratedState`](../interfaces/DehydratedState.md)\>
34β
35The dehydrated state, e.g. produced by `dehydrate` on the server.
36β
37<a id="dehydratedState-properties"></a>
38β
39#### `dehydratedState` properties
40β
41Built from [`DehydratedState`](../interfaces/DehydratedState.md#properties). See the type above for what it changes.
42β
43### options?
44β
45[`HydrateOptions`](../interfaces/HydrateOptions.md)
46β
47`defaultOptions` merged into every restored query and mutation (on top of the
48client's `defaultOptions.hydrate`), and `deserializeData` to reverse `serializeData`.
49β
50<a id="options-properties"></a>
51β
52#### `options` properties
53β
54| Property | Type | Description |
55| ------ | ------ | ------ |
56| <a id="options-property-defaultoptions"></a> `defaultOptions?` | `object` | Options applied to the queries and mutations restored from the dehydrated state. |
57| `defaultOptions.deserializeData?` | `TransformerFn` | Transforms a query's `data` after it is read from the dehydrated state, reversing `serializeData`. |
58| `defaultOptions.mutations?` | [`MutationOptions`](../interfaces/MutationOptions.md)\<`unknown`, `Error`, `unknown`, `unknown`\> | Default options merged into every mutation restored from the dehydrated state. |
59| `defaultOptions.queries?` | [`QueryOptions`](../interfaces/QueryOptions.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[], `never`\> | Default options merged into every query restored from the dehydrated state. |
60β
61## Returns
62β
63`void`
64β
65## Example
66β
67```ts
68// dehydratedState was produced by `dehydrate` on the server
69// and sent to the client, e.g. embedded in server-rendered markup.
70const queryClient = new QueryClient()
71β
72hydrate(queryClient, dehydratedState)
73```