Open History to see the last commit
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
13}
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
37}