React's useEffect is asynchronous and non-blocking: it executes after the browser paints the updated DOM to the screen. While this preserves 60 FPS UI performance for 95% of side effects, it causes visible visual flicker when measuring DOM nodes (e.g., tooltips, popovers, dropdown positions) and updating state before painting. useLayoutEffect runs synchronously before the browser paints.
useEffect vs useLayoutEffectIf you position a tooltip dynamically inside useEffect:
(0, 0).useEffect runs, measures the trigger button, and sets coordinates (250, 180).With useLayoutEffect, the coordinate calculation happens before step 2, preventing any visual jump.
Because useLayoutEffect requires direct access to DOM geometric layout engines (getBoundingClientRect, offsetHeight), calling it during Server-Side Rendering (SSR in Next.js or Node.js) generates a warning:
To silence this warning in universal codebases, conditionally alias useLayoutEffect to useEffect on the server:
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Deferred Value Updates with useDeferredValue | External Store Synchronization with useSyncExternalStore |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.