Skip to the page

Effects and synchronisation · mid

useLayoutEffect and useInsertionEffect

useLayoutEffect runs after React changes the DOM but before the browser paints, and blocks paint, so it is for measuring layout and adjusting it without a visible flicker; useEffect runs later and does not block paint.

The notes

What you must know

  • useEffect: runs after paint (usually), does not block the screen. Default choice.
  • useLayoutEffect: runs synchronously after DOM mutation, before paint. State set inside it re-renders before the user sees anything.
  • Use it for measure-then-position: tooltips, popovers, scroll position restore, auto-sizing textareas. With useEffect you can see the element jump for one frame.
  • Cost: it blocks paint, so heavy work there hurts responsiveness (INP).
  • On the server there is no layout; layout effects do not run during SSR. Measure on the client, or render that part only on the client.
  • useInsertionEffect runs before layout effects; it is for CSS-in-JS libraries to insert <style> tags before anything reads layout. App code almost never needs it.
  • Updates inside useLayoutEffect (and flushSync) are processed synchronously.

The red pen

Common traps: mistake → fix

  • Mistake: "Use useLayoutEffect everywhere to be safe"

    Fix: it blocks paint; use it only for layout reads/writes that must happen before paint.

  • Mistake: "useEffect runs before the browser paints"

    Fix: passive effects normally run after paint.

  • Mistake: "useInsertionEffect is for inserting DOM nodes"

    Fix: it is for injecting styles in CSS-in-JS libraries.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between useEffect and useLayoutEffect?
  2. Give a case where useEffect causes a visible bug.
  3. A tooltip measures its anchor and flickers. Fix it.
  4. Why does useLayoutEffect not run on the server?
  5. What is useInsertionEffect for?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview