Skip to main content

useLayoutEffect, useInsertionEffect, and useDebugValue

These Hooks solve specialized problems. They should not become default substitutes for useEffect or ordinary logging.

Three specialized responsibilities
useLayoutEffect
Read/update layout after DOM commit but before paint
useInsertionEffect
CSS-in-JS library style insertion before layout Effects
useDebugValue
Expose semantic custom-Hook state in DevTools

useLayoutEffectโ€‹

useLayoutEffect runs after React commits DOM changes and before the browser repaints.

useLayoutEffect(setup, dependencies);

Use it when the user must never see the provisional layout.

Tooltip measurement flowโ€‹

function Tooltip() {
const ref = useRef(null);
const [height, setHeight] = useState(0);

useLayoutEffect(() => {
const rect = ref.current.getBoundingClientRect();
setHeight(rect.height);
}, []);

return (
<div ref={ref} style={{ transform: `translateY(${-height}px)` }}>
Tooltip
</div>
);
}
Pre-paint layout correction
1Render provisional position
2Commit DOM
3useLayoutEffect measures
4Synchronous state update / second render
5Browser paints final position

The trade-off is clear: the user avoids a visible jump, but the browser must wait before painting.

useLayoutEffect vs useEffectโ€‹

Does this synchronization need to finish before paint?
Yes โ€” geometry/position must be correct before user sees ituseLayoutEffect may be justified
No โ€” subscription, analytics, timers, network, most integrationsPrefer useEffect
The layout can be expressed with CSS insteadAvoid measurement Effect entirely

Keep layout Effects minimal because they block visible progress.

Server renderingโ€‹

Effects do not run during server rendering. Browser layout does not exist on the server.

If a component fundamentally depends on measured layout, consider a client-only boundary, a hydration-safe initial UI, an ordinary Effect when a visible adjustment is acceptable, or CSS architecture that removes the measurement.

Strict Modeโ€‹

Development Strict Mode performs an extra setup/cleanup cycle. Cleanup must mirror setup.

useLayoutEffect(() => {
window.addEventListener('resize', measure);
return () => window.removeEventListener('resize', measure);
}, []);

Layout thrashingโ€‹

Alternating DOM reads and writes can repeatedly force layout calculation.

Batch reads and writes instead of forcing repeated layout
Risky
read width โ†’ write style โ†’ read height โ†’ write style
Better
collect required reads โ†’ compute โ†’ perform minimal writes

React cannot remove browser layout cost for you.

useInsertionEffectโ€‹

useInsertionEffect is primarily for CSS-in-JS library authors.

useInsertionEffect(setup, dependencies);

Its job is to insert runtime styles before layout Effects need to measure layout using those styles.

Why insertion timing exists
Too late
DOM update โ†’ layout measurement โ†’ style injected
Insertion Effect
style injection โ†’ layout Effects can measure with intended CSS

Example library Hook:

function useCSS(rule) {
useInsertionEffect(() => {
insertRule(rule);
}, [rule]);

return getClassName(rule);
}

This is library infrastructure, not typical application business logic.

Current useInsertionEffect caveatsโ€‹

React's current documentation states that:

  • it runs only on the client;
  • state updates are not allowed inside it;
  • refs are not attached yet;
  • you must not rely on the DOM having been updated at a specific moment;
  • cleanup/setup are interleaved one component at a time rather than globally batched like ordinary Effects.

That intentionally makes it a narrow tool.

Prefer static CSS when practicalโ€‹

React recommends static extraction or normal CSS/inline styles over runtime <style> injection when possible. Runtime injection can force repeated style recalculation and is sensitive to lifecycle timing.

useDebugValueโ€‹

useDebugValue exposes semantic state for reusable custom Hooks in React DevTools.

function useOnlineStatus() {
const online = useSyncExternalStore(
subscribe,
() => navigator.onLine,
() => true,
);

useDebugValue(online ? 'Online' : 'Offline');
return online;
}
Debug values translate implementation into domain meaning
Hook internals
subscriptions ยท refs ยท state
DevTools label
Offline / Authenticated / Loading products

Best candidates are reusable public Hooks with non-obvious internal state.

Deferred debug formattingโ€‹

If formatting is expensive:

useDebugValue(data, value => expensiveFormat(value));

The formatter can be evaluated when DevTools inspects the Hook instead of on every render.

Performance comparisonโ€‹

useLayoutEffect
Can block paint. Keep work tiny.
useInsertionEffect
Coordinates runtime styling infrastructure.
useDebugValue
Usually cheap; defer expensive formatting.

Common mistakesโ€‹

  • Using useLayoutEffect because it sounds faster.
  • Measuring layout every render without need.
  • Updating state from useInsertionEffect.
  • Using useInsertionEffect for product business logic.
  • Eagerly formatting expensive debug values.

Classification exerciseโ€‹

Which tool fits this job?
Analytics after navigation or WebSocket subscriptionuseEffect
Measure tooltip geometry before paintuseLayoutEffect
Inject CSS rules in a runtime styling libraryuseInsertionEffect
Expose readable state for a reusable custom HookuseDebugValue

Interview questionsโ€‹

Why can useLayoutEffect hurt performance?โ€‹

It and synchronous updates from it run before repaint, delaying visible progress.

What is useInsertionEffect for?โ€‹

Primarily CSS-in-JS infrastructure that must insert styles before layout Effects measure layout.

Can it update state?โ€‹

No.

What does useDebugValue do?โ€‹

It exposes a semantic label/value for custom Hooks in React DevTools.

Referencesโ€‹