Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

hydrate.md

76 lines2.8 KB

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