forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hydrate.tsx
35 lines (29 loc) · 955 Bytes
/
Hydrate.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import React from 'react'
import { hydrate, HydrateOptions } from '../core'
import { useQueryClient } from './QueryClientProvider'
export function useHydrate(state: unknown, options?: HydrateOptions) {
const queryClient = useQueryClient()
const optionsRef = React.useRef(options)
optionsRef.current = options
// Running hydrate again with the same queries is safe,
// it wont overwrite or initialize existing queries,
// relying on useMemo here is only a performance optimization.
// hydrate can and should be run *during* render here for SSR to work properly
React.useMemo(() => {
if (state) {
hydrate(queryClient, state, optionsRef.current)
}
}, [queryClient, state])
}
export interface HydrateProps {
state?: unknown
options?: HydrateOptions
}
export const Hydrate: React.FC<HydrateProps> = ({
children,
options,
state,
}) => {
useHydrate(state, options)
return children as React.ReactElement<any>
}