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

Hydrate.tsx

32 lines962 Bytes

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])
22​
23export interface HydrateProps {
24 state?: unknown
25 options?: HydrateOptions
26 children?: React.ReactNode
28​
29export const Hydrate = ({ children, options, state }: HydrateProps) => {
30 useHydrate(state, options)
31 return children as React.ReactElement