Open History to see the last commit
1'use client'
2
3import { useRef } from 'react'
4import { useDbClient } from './DbProvider'
5import type { DehydratedDbState } from '@tanstack/db'
6import type { ReactNode } from 'react'
7
8export type HydrationBoundaryProps = {
9 state: DehydratedDbState
10 children?: ReactNode
11}
12
13export function HydrationBoundary({ state, children }: HydrationBoundaryProps) {
14 const client = useDbClient()
15 const hydrated = useRef<
16 { client: typeof client; state: DehydratedDbState } | undefined
17 >(undefined)
18
19 if (hydrated.current?.client !== client || hydrated.current.state !== state) {
20 client.hydrate(state)
21 hydrated.current = { client, state }
22 }
23
24 return children
25}