useHydrated
false on the server and during hydration, true from the next render on. Hold back browser-only output without a hydration mismatch.
useHydrated tells you whether the component is past hydration. Use it to hold back anything that only the browser can render — a value read from window, a widget that measures the DOM, a third-party embed — until React can show it without a hydration mismatch.
import { useHydrated } from '@42-central/ui-react';
const hydrated = useHydrated();
return hydrated ? <LiveClock /> : <ClockSkeleton />;Usage
The viewport size below only exists in the browser. On a server-rendered page the first paint shows …, the same text the server sent, then the real size replaces it.
Server render: nothing to read yet
…
Code
const hydrated = useHydrated();
<p>{hydrated ? `${window.innerWidth} × ${window.innerHeight} viewport` : '…'}</p>A ClientOnly wrapper is three lines on top of it:
function ClientOnly({ children, fallback = null }: { children: ReactNode; fallback?: ReactNode }) {
return useHydrated() ? children : fallback;
}API
function useHydrated(): boolean| Where it renders | Returns |
|---|---|
| On the server | false |
| The hydration render in the browser | false |
| Every render after that | true |
| An app rendered only in the browser (no SSR) | true from the first render |
How it works
It is useSyncExternalStore over a store that never changes: the server snapshot is false, the client snapshot is true. React uses the server snapshot for the server render and for hydration, so both emit the same markup, then re-renders with the client snapshot straight away. Unlike a useState(false) + useEffect pair, a component that mounts after hydration (a modal opened later, a client-side route change) gets true on its first render, with no extra frame of fallback.
The kit's own SSR-safe hooks (useMediaQuery, useApplePlatform, useNow) follow the same pattern with a real value in place of true. If what you need is one browser value, prefer a hook that returns it directly.