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