Open History to see the last commit
1---
2id: HydrationBoundary
3title: HydrationBoundary
4---
5β
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7β
8```ts
9function HydrationBoundary(props: HydrationBoundaryProps): ReactElement<unknown, string | JSXElementConstructor<any>>;
10```
11β
12Defined in: [packages/react-query/src/HydrationBoundary.tsx:85](https://github.com/TanStack/query/blob/main/packages/react-query/src/HydrationBoundary.tsx#L85)
13β
14`HydrationBoundary` adds a previously dehydrated state into the `queryClient` that would be returned by
15`useQueryClient()`. If the client already contains data, the new queries will be intelligently merged based on
16update timestamp.
17β
18Note: Only `queries` can be dehydrated with an `HydrationBoundary`.
19β
20## Parameters
21β
22### props
23β
24[`HydrationBoundaryProps`](../interfaces/HydrationBoundaryProps.md)
25β
26The dehydrated `state` to hydrate, the hydrate `options`, an optional custom
27`queryClient`, and the `children` to render.
28β
29<a id="props-properties"></a>
30β
31#### `props` properties
32β
33| Property | Type | Description |
34| ------ | ------ | ------ |
35| <a id="props-property-children"></a> `children?` | `ReactNode` | The components to render β always rendered unconditionally, not gated on hydration. New queries are hydrated into the cache during render; for queries that already exist in the cache, only newer dehydrated data is hydrated, and that happens in an effect after commit, so `children` may render briefly before it lands. |
36| <a id="props-property-options"></a> `options?` | [`OmitKeyof`](../type-aliases/OmitKeyof.md)\<[`HydrateOptions`](../interfaces/HydrateOptions.md), `"defaultOptions"`\> & `object` | Optional. Note: unlike `hydrate`, `mutations` cannot be set here. |
37| <a id="props-property-queryclient"></a> `queryClient?` | [`QueryClient`](../classes/QueryClient.md) | Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will be used. |
38| <a id="props-property-state"></a> `state` | [`DehydratedState`](../interfaces/DehydratedState.md) \| `null` \| `undefined` | The state to hydrate. |
39β
40## Returns
41β
42`ReactElement`\<`unknown`, `string` \| `JSXElementConstructor`\<`any`\>\>
43β
44The provided `children`, rendered unconditionally. New queries in `state` are hydrated into the
45cache during render; for queries already in the cache, only newer dehydrated data is hydrated, in an effect
46after commit.
47β
48## Examples
49β
50```tsx
51import { HydrationBoundary } from '@tanstack/react-query'
52β
53function App() {
54 return <HydrationBoundary state={dehydratedState}>...</HydrationBoundary>
55}
56```
57β
58Server-side prefetch handed off to the client via `dehydrate`:
59```tsx
60import { HydrationBoundary, dehydrate, noop } from '@tanstack/react-query'
61β
62async function ServerComponent() {
63 const queryClient = getQueryClient()
64β
65 await queryClient
66 .query({
67 queryKey: ['posts'],
68 queryFn: fetchPosts,
69 })
70 .catch(noop)
71β
72 return (
73 <HydrationBoundary state={dehydrate(queryClient)}>
74 <Posts />
75 </HydrationBoundary>
76 )
77}
78```