HydrationBoundary
TheHydrationBoundary component is used to hydrate queries that were prefetched on the server into the QueryClient cache on the client side. This is essential for Server-Side Rendering (SSR) scenarios.
Import
Props
DehydratedState | null | undefined
required
The dehydrated state from the server. Typically obtained from
dehydrate(queryClient) on the server.HydrateOptions
React.ReactNode
Child components that will have access to the hydrated queries
QueryClient
Optional QueryClient instance. If not provided, uses the client from context.
Examples
Next.js App Router (Server Components)
Next.js Pages Router (getServerSideProps)
Remix Loader
Multiple Boundaries
Nested Boundaries
With Custom Options
Handling Null State
How It Works
- Server Side: Use
dehydrate(queryClient)to serialize the cache into a plain object - Transfer: Pass this dehydrated state from server to client (via props, loaders, etc.)
- Client Side:
HydrationBoundarycallshydrate()to restore the cache - Optimization: New queries are hydrated immediately during render, while existing queries are hydrated in an effect to avoid updating current page data during transitions
Notes
- The
stateprop can benullorundefined- the component handles this gracefully - Hydration only occurs if the query doesn’t already exist in the cache or if the hydrated data is newer
- Multiple
HydrationBoundarycomponents can be used for different parts of your app - The component performs intelligent hydration timing:
- New queries (not in cache) are hydrated during render
- Existing queries are hydrated in an effect to support React transitions
- If a query already exists with newer data, hydration is skipped for that query
- The component returns children as-is (it’s a transparent wrapper)
- Must be used within a
QueryClientProvider