← Back to React, page 1

react-0013 · Hard senior

What is the difference between useEffect and useLayoutEffect, and when do you need the second one?

Both run after React updates the DOM. useLayoutEffect runs synchronously before the browser paints, so code in it can measure the layout and change state without the user seeing a flash. useEffect usually runs after paint, so it never delays the first visual update. Use useEffect by default and useLayoutEffect only to measure or adjust layout before paint.

Full answer

React's update has two stages. In the render phase it calls your components and works out what changed. In the commit phase it applies the changes to the DOM. After the commit, the browser paints pixels to the screen.

useLayoutEffect runs right after the DOM is updated and before the browser paints. React runs it synchronously, so the browser waits. A state update inside it is processed immediately and the screen is painted once, with the final result.

useEffect runs after the paint in most cases. The user may see the first result for a moment and then an updated one. For a data fetch or a subscription this is fine, and it keeps the page responsive.

A typical need for useLayoutEffect is measurement. A tooltip must know the height of its content before choosing whether to appear above or below the trigger. If that measurement happens in useEffect, the tooltip is painted in the wrong place and then jumps. Scroll position restoration and focus handling for animations have the same shape.

Cost matters. Work inside useLayoutEffect blocks painting, so slow code there makes the interface feel stuck. Keep it short and use it only when you see a flicker that useEffect cannot avoid.

The order within one commit is: render, DOM update, layout effects, browser paint, then passive effects (useEffect). A child's effects run before its parent's. Cleanups run before the next run of the same effect.

React 18 also has a nuance: a useEffect caused by a discrete event such as a click may be flushed before paint. Do not depend on exact timing of useEffect. If order relative to paint matters, use useLayoutEffect.

A related hook, useInsertionEffect, runs even earlier and is meant only for CSS-in-JS libraries that insert style rules.

jsx
import { useEffect, useLayoutEffect, useRef, useState } from 'react';

export default function Panel() {
  const boxRef = useRef(null);
  const [height, setHeight] = useState(0);
  console.log('render');

  useLayoutEffect(() => {
    console.log('layout effect');
    setHeight(boxRef.current.offsetHeight);
  }, []);

  useEffect(() => {
    console.log('effect');
  }, []);

  return (
    <div ref={boxRef}>
      <p>Measured height: {height}px</p>
    </div>
  );
}

The log shows the order: render, layout effect, then the passive effect. This was run in jsdom, which does no layout, so offsetHeight is 0 and setHeight(0) changes nothing, which is why no second render appears. In a real browser the height is not zero, so the state update triggers a second render before the first paint. Output of the run: render layout effect effect

Key points

  • useLayoutEffect fires after DOM changes and before paint, synchronously. useEffect fires after paint in most cases.
  • Use useLayoutEffect to measure layout or fix position without a visible flicker.
  • Heavy work in useLayoutEffect blocks painting and hurts responsiveness.
  • Order in one commit: render, layout effects, paint, passive effects. Children run before parents.
  • Start with useEffect and switch only when you can see the flash.

Common mistakes

  • Using useLayoutEffect for data fetching or subscriptions, which only blocks paint.
  • Doing slow computation inside useLayoutEffect.
  • Assuming useEffect never runs before paint in every case.
  • Measuring the DOM in useEffect and causing a visible flicker.

Follow-ups

  • Why does a state update inside useLayoutEffect not cause a visible intermediate frame?
  • What is useInsertionEffect for?
  • How do effects behave during server rendering?

Tips

  • Give the tooltip or measurement example straight away. It proves you have seen a real flicker.
  • Describe the order as render, DOM update, layout effects, paint, effects.
  • Say "default to useEffect". It signals judgement rather than a love of the more powerful hook.