-
How do you decide when to run
useLayoutEffect
?Ans:
useLayoutEffect
is a version ofuseEffect
that fires before the browser repaints the screen.- Measuring layout before the browser repaints the screen.
- Blocks the browser from repainting.
-
The execution order of
useEffect
anduseLayoutEffect
.Feature useEffect useLayoutEffect Execution Timing Runs after the render is committed to the screen. This means it runs after the browser has painted changes to the screen. Runs synchronously after all DOM mutations. This means it runs before the browser has a chance to paint, making it suitable for reading layout from the DOM and synchronously re-rendering. Use Cases Most side effects (data fetching, subscriptions) DOM manipulation, measuring elements, critical layout updates Performance Generally better performance due to async nature Can impact performance if used excessively, as it blocks the browser from repainting
Taobao HangZhou Interview Round 1
· 3 min read