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

HydrationBoundary.tsx

37 lines1.0 KB

1'use client'
2import * as React from 'react'
3​
4import type { HydrateOptions, QueryClient } from '@tanstack/query-core'
5import { hydrate } from '@tanstack/query-core'
6import { useQueryClient } from './QueryClientProvider'
7​
8export interface HydrationBoundaryProps {
9 state?: unknown
10 options?: HydrateOptions
11 children?: React.ReactNode
12 queryClient?: QueryClient
14​
15export const HydrationBoundary = ({
16 children,
17 options = {},
18 state,
19 queryClient,
20}: HydrationBoundaryProps) => {
21 const client = useQueryClient(queryClient)
22​
23 const optionsRef = React.useRef(options)
24 optionsRef.current = options
25​
26 // Running hydrate again with the same queries is safe,
27 // it wont overwrite or initialize existing queries,
28 // relying on useMemo here is only a performance optimization.
29 // hydrate can and should be run *during* render here for SSR to work properly
30 React.useMemo(() => {
31 if (state) {
32 hydrate(client, state, optionsRef.current)
33 }
34 }, [client, state])
35​
36 return children as React.ReactElement