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