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

HydrationBoundary.md

76 lines2.8 KB

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