useEffect vs useLayoutEffect: Stop Tooltip Flicker
Краткое содержание
Pixel shares a DEV Community guide comparing useEffect and useLayoutEffect in React. It explains how useLayoutEffect measures elements with getBoundingClientRect and corrects tooltip position before the browser paints, while useEffect runs afterward for tasks like fetching or subscribing.
DEV Community
useEffect vs useLayoutEffect: Why the Tooltip Flashes, Then Jumps
Picture a tooltip that should sit above a button. For one frame it shows up at the top of the box, then it jumps into place. The measurement was right. It ran too late. useEffect let the browser paint the first, wrong position. The mental model: before the curtain, or after React updates the DOM, then the browser paints what the user sees. There is a gap between those two, like the moment before a curtain opens. useLayoutEffect runs in that gap, while the curtain is closed. useEffect runs after the curtain opens, once the user can already see the frame. Both take the same arguments: a setup function, a dependency list, and an optional cleanup. The only difference is when the setup runs. One update, from commit to paint 1. React writes the DOM — The new elements are in the document
2. useLayoutEffect — Measure and correct, still invisible
3. Browser paints — This is the frame the user sees
4. useEffect — Fetch, subscribe, log
The user only sees the frame after useLayoutEffect has finished. How it works useEffect is the one for work that talks to the outside world: a fetch, a subscription, a timer, a log line. The browser paints first, so the effect doesn't hold the frame up. The user sees the new UI, then the effect runs. useLayoutEffect is for the case where that first paint would be a lie. You need a number the browser only knows after layout, such as a height from getBoundingClientRect, and you need to change what's on screen with it. useEffect(() => {
const { height } = tipRef.current.getBoundingClientRect();
setTop(buttonBottom - height); // one frame at top: 0, then it jumps
}, [buttonBottom]);
Swap in useLayoutEffect and that setTop is flushed before the browser paints. React may render the tooltip twice, first at top: 0 and then at the measured spot. The user only sees the second render. That correction is also the cost. The code inside useLayoutEffect, and any state update it schedules, blocks the paint. A slow one delays the frame. The React docs say to prefer useEffect unless that blocking is the point. The dependency list works the same on both. It still decides when the effect re-runs, and a stale value is still stale. Timing doesn't change that. The rules are in useEffect dependencies. In practice Start with useEffect. Move to useLayoutEffect only after you've seen a flash. The flash shows up when you read layout and then write the screen: A tooltip or menu that must flip above or below, depending on the space left. A textarea whose height follows its content. Scrolling or focusing an element before the user sees it in the wrong place. Leave fetching, subscriptions, and logging on useEffect. They don't change the frame that's about to paint. Running them in useLayoutEffect makes that paint wait for work the user didn't need to wait for. Honest stakes: most components never measure the DOM. You'll want useLayoutEffect a handful of times, usually on something floating. Reaching for it to be safe makes the first paint slower. Wait until something flickers. Say it in an interview "useEffect and useLayoutEffect take the same arguments. useLayoutEffect runs after React commits DOM changes and before the browser paints, so you can measure layout and correct it without a visible flash. useEffect runs after the paint, so it doesn't block the frame. State updates inside useLayoutEffect are flushed before that paint. Default to useEffect, and switch only when the user would otherwise see one wrong frame." Cheat sheet useEffect useLayoutEffect When it runs After the browser paints After the DOM update, before the paint Blocks the paint? No Yes Use it for Fetch, subscribe, log Measure layout, then fix what the user would see A state update inside Shows up on a later frame Flushed before the paint The default This one Only after you've seen a flash TL;DR useLayoutEffect runs before the paint. useEffect runs after it. The layout version can measure the DOM and correct it before anyone sees the...

Powered by Readybot.io