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