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

dehydrate.md

72 lines3.0 KB

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```