The two hooks take the same arguments and follow the same dependency and cleanup rules. Only the timing differs.
useLayoutEffect runs inside the commit: after React has changed the DOM and attached refs, before the browser can paint. Any state update it schedules is rendered and committed synchronously, still before paint, and React runs pending useEffect callbacks first. So the user only ever sees the final result.
useEffect usually runs in a later task, and the browser may paint in between. If the effect corrects something visible, that painted frame is the flicker.
Use the layout version when you must read layout and render based on it before the user sees anything:
- measure a tooltip or popover, then place it above or below its anchor;
- restore or adjust a scroll position after new content renders;
- synchronise DOM state that must never be shown half-done.
The cost. The browser can't paint until the layout effect and its updates finish, so slow work there delays every frame of that update. Measuring also forces the browser to compute style and layout synchronously. Prefer useEffect; switch only for a visible flicker.
Server rendering. Neither hook runs on the server. React 19 removed the old "useLayoutEffect does nothing on the server" warning. The underlying problem remains: the server HTML can't contain the measured result, so it jumps after hydration. Render such components only on the client, or accept useEffect.
useInsertionEffect runs even earlier, before any layout effect. It exists for CSS-in-JS libraries that inject <style> tags at runtime, so styles are in place before layout effects read layout. Refs aren't attached yet when it runs, it can't schedule updates, and it may run before or after the DOM is updated. Application code shouldn't need it.