Open History to see the last commit
1---
2id: dehydrate
3title: dehydrate
4---
5β
6```ts
7function dehydrate(client: QueryClient, options?: DehydrateOptions): DehydratedState;
8```
9β
10Defined in: [packages/query-core/src/hydration.ts:245](https://github.com/TanStack/query/blob/main/packages/query-core/src/hydration.ts#L245)
11β
12Dehydrates a `QueryClient`'s cache (queries and mutations) into a plain, serializable `DehydratedState`,
13typically to embed in server-rendered markup and later restore into a client-side `QueryClient` via `hydrate`.
14Which queries/mutations are included, and how their data/errors are transformed, is controlled by `options`,
15falling back to the client's `dehydrate` default options, and finally to `defaultShouldDehydrateQuery` /
16`defaultShouldDehydrateMutation`.
17β
18## Parameters
19β
20### client
21β
22[`QueryClient`](../classes/QueryClient.md)
23β
24The client whose cache is dehydrated.
25β
26### options?
27β
28[`DehydrateOptions`](../interfaces/DehydrateOptions.md) = `{}`
29β
30Controls which queries and mutations are included and how their data and errors
31are transformed. Each option falls back to the client's `defaultOptions.dehydrate`.
32β
33<a id="options-properties"></a>
34β
35#### `options` properties
36β
37| Property | Type | Description |
38| ------ | ------ | ------ |
39| <a id="options-property-serializedata"></a> `serializeData?` | `TransformerFn` | Transforms a query's `data` before it is dehydrated. Useful for non-JSON-serializable data. |
40| <a id="options-property-shoulddehydratemutation"></a> `shouldDehydrateMutation?` | (`mutation`: [`Mutation`](../classes/Mutation.md)) => `boolean` | Predicate to decide whether a given `Mutation` should be dehydrated. Defaults to `defaultShouldDehydrateMutation`. |
41| <a id="options-property-shoulddehydratequery"></a> `shouldDehydrateQuery?` | (`query`: [`Query`](../classes/Query.md)) => `boolean` | Predicate to decide whether a given `Query` should be dehydrated. Defaults to `defaultShouldDehydrateQuery`. |
42| <a id="options-property-shouldredacterrors"></a> `shouldRedactErrors?` | (`error`: `unknown`) => `boolean` | Predicate to decide whether a query's error should be redacted before dehydration. Errors are redacted (replaced with a generic `Error('redacted')`) unless this function is provided and returns `false` for the given error, in which case the original error is kept. |
43β
44## Returns
45β
46[`DehydratedState`](../interfaces/DehydratedState.md)
47β
48The dehydrated state, with the included `queries` and `mutations`.
49β
50<a id="result-properties"></a>
51β
52### Result properties
53β
54| Property | Type | Description |
55| ------ | ------ | ------ |
56| <a id="result-property-mutations"></a> `mutations` | `DehydratedMutation`[] | The dehydrated mutations, by default only the paused ones. |
57| <a id="result-property-queries"></a> `queries` | `DehydratedQuery`[] | The dehydrated queries, by default only the successful ones. |
58β
59## Example
60β
61```ts
62const queryClient = new QueryClient()
63β
64await queryClient.prefetchQuery({
65 queryKey: ['posts'],
66 queryFn: getPosts,
67})
68β
69const dehydratedState = dehydrate(queryClient)
70```