Layout Effects and Measuring Before Paint
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Measure the DOM before the browser paints.
- Choose between useEffect and useLayoutEffect.
- Explain where useInsertionEffect belongs.
Overview
#Before paint versus after paint
useEffect runs after the browser has painted. useLayoutEffect runs after React commits the DOM but before the browser paints. That timing matters when you must read layout and adjust it without the user seeing an intermediate frame.
useLayoutEffect(() => {
const height = ref.current!.getBoundingClientRect().height;
setHeight(height);
}, [content]);
Because the state update happens before paint, React re-renders synchronously and the user never sees the wrong size.
When to use it
Use useLayoutEffect to measure the DOM, to scroll to a position, or to synchronously reposition an element. Use useEffect for everything that does not need to block paint: subscriptions, timers, network requests, logging.
useInsertionEffect
useInsertionEffect runs before layout effects and is intended for CSS-in-JS libraries that inject style rules. Application code almost never needs it. It cannot read the DOM or schedule updates.
Pitfall: layout effects block paint
useLayoutEffect is synchronous and blocks the browser from painting. A slow layout effect makes the page feel frozen. Keep it small and measure only what you need.
Server rendering warning
useLayoutEffect does nothing on the server and React warns when it runs during server rendering. If a component must render on the server, guard the effect or move the logic to useEffect when the visual jump is acceptable.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A tooltip measures its size in useLayoutEffect and sets state. Does the user see a flash of the wrong position?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsxuseLayoutEffect(() => { setPos(measure(ref.current)); }, [target]);Exercise 2Implement
Measure a box and store its width using a layout effect so no wrong width is painted.
starter.tsxexport function Box({ children }: { children: React.ReactNode }) { return <div>{children}</div>; }Reveal the reference answer
reference.tsximport { useLayoutEffect, useRef, useState } from 'react'; export function Box({ children }: { children: React.ReactNode }) { const ref = useRef<HTMLDivElement>(null); const [width, setWidth] = useState(0); useLayoutEffect(() => { if (ref.current) setWidth(ref.current.getBoundingClientRect().width); }, [children]); return <div ref={ref}>{children} ({width}px)</div>; }Authored expected output: The width is measured before paint, so no zero-width frame is shown.
Reading getBoundingClientRect in a layout effect happens after the DOM is committed but before paint. Setting state there re-renders synchronously, so the first painted frame already has the measured width.
Exercise 3Debug
This tooltip flickers to the top-left before snapping into place. Explain and fix.
starter.tsxuseEffect(() => { setPos(computePosition(anchorRef.current, tooltipRef.current)); }, [anchor]);Reveal the reference answer
reference.tsxuseLayoutEffect(() => { setPos(computePosition(anchorRef.current, tooltipRef.current)); }, [anchor]);Authored expected output: The tooltip appears in its final position with no flicker.
useEffect runs after paint, so the first frame shows the default position. useLayoutEffect runs before paint, so the corrected position is applied before the user sees anything.
Exercise 4Explain
Explain the difference between useEffect and useLayoutEffect and when each is appropriate.
starter.tsx// useEffect vs useLayoutEffectReveal the reference answer
reference.tsxuseEffect runs after the browser paints, so it never blocks rendering and is right for subscriptions, timers, data fetching and logging. useLayoutEffect runs after the DOM is committed but before paint, so it is right when you must measure or reposition the DOM and cannot tolerate an intermediate frame. Because it blocks paint, keep it small. Prefer useEffect unless the visual timing forces useLayoutEffect.Authored expected output: A written comparison of timing, paint blocking and appropriate use cases.
The deciding factor is whether the user would see a wrong frame. If yes, use a layout effect; otherwise use a normal effect.
Common misconceptions
#- useLayoutEffect is always faster than useEffect. It runs earlier but blocks paint; use it only when timing requires it.
- useLayoutEffect runs on the server. It has no server behavior and React warns.
- useInsertionEffect is for measuring the DOM. It is for injecting styles and cannot read layout.
- A layout effect can be as slow as you like. It blocks paint, so slow work makes the page freeze.
Follow-up practice
#- react-animation
- react-portals
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.