Capítulo 58 de 116
useLayoutEffect(setup, dependencies?) fires synchronously after DOM mutations but before the browser paints — the tool for reading layout (measuring an element) and then synchronously adjusting it before the user ever sees the unadjusted version.
useEffect (setup function, optional cleanup, dependency array with identical semantics) — the difference is purely timing.useEffect, which runs after paint and doesn't block it.useEffect, the user would briefly see the un-flipped tooltip flash before the correction, since the browser would have already painted.useLayoutEffect body directly slows down perceived responsiveness — use it only when the visible flash it prevents would actually be a problem, not as a default over useEffect.useLayoutEffect doesn't run during server rendering (there's no browser layout to read) — React warns if it's used in a component that renders on the server without a client-only guard, since its effect would be missing on the server-rendered HTML.useLayoutEffect(() => {
const { height } = ref.current.getBoundingClientRect();
setTooltipHeight(height); // synchronous, before paint — avoids a visible flash
}, []);
useEffect would cause.useEffect; reach for useLayoutEffect only when skipping it would cause a visible flash/flicker from a measure-then-adjust sequence.useEffect.