useEffect vs useLayoutEffect
Browser Rendering Pipeline
Why This Is Asked
This tests deep knowledge of React’s rendering pipeline and the browser paint cycle. Interviewers want to know if you understand when each hook fires relative to the browser paint, and whether you can identify the specific scenarios where useLayoutEffect is necessary (DOM measurements, flicker prevention).
Key Concepts
useEffectfires asynchronously after the browser has painted — it does not block visual updatesuseLayoutEffectfires synchronously after DOM mutations but before the browser paints- Use
useLayoutEffectwhen you need to measure or mutate the DOM before the user sees it (e.g., tooltips, scroll position, animations) useLayoutEffectblocks the paint, so heavy computation in it causes visible jankuseLayoutEffectdoesn’t run on the server — using it in SSR components triggers warnings