useLayoutEffect, useInsertionEffect, and useDebugValue
These Hooks solve specialized problems. They should not become default substitutes for useEffect or ordinary logging.
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>
);
}
The trade-off is clear: the user avoids a visible jump, but the browser must wait before painting.
useLayoutEffect vs useEffectโ
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.
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.
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;
}
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โ
Common mistakesโ
- Using
useLayoutEffectbecause it sounds faster. - Measuring layout every render without need.
- Updating state from
useInsertionEffect. - Using
useInsertionEffectfor product business logic. - Eagerly formatting expensive debug values.
Classification exerciseโ
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.