useLayoutEffect for Pre-Animation Measurements
Use useLayoutEffect to measure element dimensions before animations begin to avoid layout jumps.
The Flash Problem
Sometimes an element visibly jumps or flickers on its first render before settling into place. This usually happens when you measure or reposition an element after the browser has already painted it.
The user sees the wrong position for one frame, then a sudden correction, which looks broken.
Why useEffect Causes the Flash
useEffect runs after the browser paints. So if you read a measurement and update the DOM inside useEffect, the user has already seen the un-corrected frame before your change applies.
For visual positioning that must be invisible to the user, after-paint timing is too late.
All lessons in this course
- CSS Transitions with React State Changes
- CSS Keyframe Animations with React
- useLayoutEffect for Pre-Animation Measurements
- Enter and Exit Animations Without a Library