React · Frontend system thinking

When would you use useLayoutEffect instead of useEffect?

HarduseLayoutEffectuseEffectlayoutUpdated Oct 2026
Quiz this track

Short answer

useLayoutEffect runs synchronously after DOM mutations but before the browser paints. Use it when you must measure or adjust layout to avoid a visible flicker; use useEffect for everything else.

Both effects run after React commits DOM changes. The difference is timing. useLayoutEffect runs before the browser paints, so a state update inside it is applied before the user sees the frame. useEffect runs after paint, which is usually what you want because it does not block rendering.

jsx
function Tooltip({ targetRef, children }) {
  const tooltipRef = useRef(null);
  const [position, setPosition] = useState({ top: 0, left: 0 });

  useLayoutEffect(() => {
    const target = targetRef.current;
    const tooltip = tooltipRef.current;
    if (!target || !tooltip) return;

    const rect = target.getBoundingClientRect();
    setPosition({ top: rect.bottom + 8, left: rect.left });
  }, [targetRef]);

  return (
    <div ref={tooltipRef} style={{ position: "fixed", ...position }}>
      {children}
    </div>
  );
}
Measuring before paint lets the tooltip appear in the right place on its first visible frame.

How to choose

  • Default to useEffect; it keeps the browser responsive
  • Use useLayoutEffect for DOM measurements that must affect the next paint
  • Avoid expensive synchronous work in useLayoutEffect because it blocks painting
  • Remember that useLayoutEffect does not run during server rendering

A common mistake is reaching for useLayoutEffect to "run earlier" without a layout reason. That can make the app feel slower and can trigger server-rendering warnings. If the work does not need to happen before paint, it belongs in useEffect.

useInsertionEffect is a separate, lower-level hook for CSS-in-JS libraries that need to inject styles before layout effects read the DOM. Application code rarely needs it.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why does useLayoutEffect block painting?
  2. 2What happens when useLayoutEffect runs during server rendering?
  3. 3How do you measure an element without causing a visible flicker?
  4. 4What is useInsertionEffect used for?

Say it in the interview

useLayoutEffect runs after the DOM is updated but before the browser paints, so it is for measurements or adjustments that must be visible in the same frame, such as positioning a tooltip. useEffect runs after paint and is the default for subscriptions and other side effects. I avoid useLayoutEffect unless a layout read or write actually needs to happen before the user sees the frame.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live React mock interview and get scored feedback on depth, clarity and confidence.