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.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe 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
useEffectyou 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.
useInsertionEffectruns 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(andflushSync) are processed synchronously.
The red pen
Common traps: mistake → fix
Mistake: "Use
useLayoutEffecteverywhere to be safe"Fix: it blocks paint; use it only for layout reads/writes that must happen before paint.
Mistake: "
useEffectruns before the browser paints"Fix: passive effects normally run after paint.
Mistake: "
useInsertionEffectis for inserting DOM nodes"Fix: it is for injecting styles in CSS-in-JS libraries.
Easy to hard
Questions interviewers ask, easy to hard
- What is the difference between
useEffectanduseLayoutEffect? - Give a case where
useEffectcauses a visible bug. - A tooltip measures its anchor and flickers. Fix it.
- Why does
useLayoutEffectnot run on the server? - What is
useInsertionEffectfor?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.