Skip to main content

HydrationBoundary

The HydrationBoundary 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
Optional hydration options
object
Default options to apply to hydrated queries
QueryOptions
Default query options
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

  1. Server Side: Use dehydrate(queryClient) to serialize the cache into a plain object
  2. Transfer: Pass this dehydrated state from server to client (via props, loaders, etc.)
  3. Client Side: HydrationBoundary calls hydrate() to restore the cache
  4. 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 state prop can be null or undefined - 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 HydrationBoundary components 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