Sub topic 16
useLayoutEffect and when timing matters
This chapter assumes you know useEffect, dependency arrays and cleanup functions from the earlier chapters. It also assumes you know that React renders first and then commits changes to the DOM.
Where each effect runs
useEffect runs after React has committed changes to the DOM and the browser has had a chance to paint. Most effects do not care about this timing. A few do. If your effect measures the DOM and then changes state, the user may briefly see the unmeasured layout before the corrected one. This shows up as a flicker.
useLayoutEffect has the same signature as useEffect. The difference is timing. It runs synchronously after the DOM is updated and before the browser paints. If you set state inside it, React re-renders immediately, and the user only sees the final result.
A case where timing matters
A tooltip must sit above its anchor, but its height is only known after it is in the DOM. Measuring in useEffect would paint the tooltip in the wrong spot for one frame. Measuring in useLayoutEffect avoids that.
import { useLayoutEffect, useRef, useState } from 'react';
function Tooltip({ text, children }) {
const tipRef = useRef(null);
const [top, setTop] = useState(0);
useLayoutEffect(() => {
const height = tipRef.current.getBoundingClientRect().height;
setTop(-height - 4);
}, [text]);
return (
<span style={{ position: 'relative' }}>
{children}
<span
ref={tipRef}
role="tooltip"
style={{ position: 'absolute', top, left: 0, whiteSpace: 'nowrap' }}
>
{text}
</span>
</span>
);
}The first render places the tooltip at top: 0. The layout effect measures it, sets top, and React re-renders before the paint. The user never sees the first position.
Rules of thumb
- Start with
useEffect. Switch touseLayoutEffectonly when you can see a visual flicker, or when you must read layout and write to the DOM before the user sees the result. - Typical uses are measuring elements, positioning popovers, and restoring scroll position.
- Keep the body short. The browser cannot paint while a layout effect runs, so slow work makes the page feel frozen.
- Do not fetch data, start timers or log analytics here. None of these need to block painting.
- Cleanup works the same way as in
useEffect. Layout effect cleanups also run before the next paint.
Gotchas
- Server rendering. Effects do not run on the server, but React warns when
useLayoutEffectappears in a component that renders on the server. Server-rendered HTML has no layout to measure. Move the logic touseEffect, or render the component only on the client. - Order. Within one commit, all layout effects run before any
useEffect. Cleanups run before the new effects. - Extra render cost. Setting state in a layout effect forces a second render before paint. That is intended, but it is wasted work if CSS could do the job. Check whether a CSS-only solution exists first.
Summary
useLayoutEffect is a narrow tool. It is for effects that read or change layout and must finish before the user sees the frame. For everything else, useEffect is the right choice.
Exercise
A component measures a div's width in useEffect and stores it in state. Users see the layout jump on every page load. What change would you try, and why?
Show answer
Replace useEffect with useLayoutEffect. It runs after the DOM update but before the browser paints. The state update then triggers a re-render before the user sees the first, unmeasured layout. If the component also renders on the server, expect a warning about useLayoutEffect and handle that case.