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