'use client' import { useLayoutEffect } from 'react' import { dehydrateDataKey, dehydrateDataValueKey, } from 'rntwsc/hydration/config' import type { DehydrateProps } from 'rntwsc/hydration/dehydrate' import { dehydrated, rehydrated } from 'rntwsc/hydration/dehydrate' import { setHydration } from 'rntwsc/hydration/store' import { isBrowser } from 'rntwsc/platform' import { useIsMounted } from 'rntwsc/libs/hooks' import { jsonSafe } from 'rntwsc/libs/json-safe' type Props = Required & { // pass from the caller instead of module level isServer: boolean } export const DehydrateTemplate = ({ k, v, isServer }: Props) => { // notify in use effect useLayoutEffect(() => { if (isServer) { setHydration(k, v) } }, [k, v, isServer]) // remove it from dom if mounted const mounted = useIsMounted() if (mounted) { return null } // to not dehydrate a key twice const s = dehydrated() if (s.has(k)) { return null } s.add(k) // to not dehydrate on browser if this key has no server counterpart if (isBrowser && !rehydrated.has(k)) { return null } // render template instead of script as latest react recommendation const props = { [dehydrateDataKey]: k, [dehydrateDataValueKey]: jsonSafe(v), } return