useIsomorphicLayoutEffect
Layout effect that works under SSR.
Import
TypeScript
import { useIsomorphicLayoutEffect } from '@/hooks';API
Signature
useIsomorphicLayoutEffect(effect, deps?)
When to use
Use useIsomorphicLayoutEffect instead of re-implementing the same pattern in multiple components.
SSR / Next.js
Measurements are 0 / empty until the element is mounted.
Live playground
Edit the example and see it run against the real @/hooks package in this app.
Live playgroundEdit · run with real
@/hooks · useIsomorphicLayoutEffectEditor
function Measure({ id }: { id: string }) { useIsomorphicLayoutEffect(() => { const el = document.getElementById(id); console.log(el?.getBoundingClientRect()); }, [id]); return null; }
Preview
Implementation source
Full library source for useIsomorphicLayoutEffect — copy into your project when you are not installing a package yet.
hooks/dom/useIsomorphicLayoutEffect.ts
export { useIsomorphicLayoutEffect } from '../utility/useIsomorphicLayoutEffect';
Copy-paste example
useIsomorphicLayoutEffect.example.tsx
'use client';
import { useIsomorphicLayoutEffect } from '@/hooks';
export function Measure({ id }: { id: string }) {
useIsomorphicLayoutEffect(() => {
const el = document.getElementById(id);
console.log(el?.getBoundingClientRect());
}, [id]);
return null;
}Production checklist
- Called only from Client Components or other custom hooks
- Type parameters filled in (e.g.
useFetch<User[]>) - Tip: Attach the ref before measuring. ResizeObserver requires a mounted element.
- Source of truth: open
hooks/dom/useIsomorphicLayoutEffect.tsfor implementation details