Skip to main content

What does useLayoutEffect do?

What useLayoutEffect() does

The useLayoutEffect() hook lets you run side effects right after React has updated the DOM, but before the browser "shows" those changes to the user.

In simpler terms: useLayoutEffect runs between the "render" phase and the "paint" phase. This gives you a chance to "catch the DOM" and do something with it before the user sees the result.


Syntax

javascript
useLayoutEffect(() => { // code that works with the DOM return () => { // cleanup }; }, [dependencies]);

Difference between useEffect and useLayoutEffect

PropertyuseEffectuseLayoutEffect
When it runsAfter the browser paintsBefore painting
Does it block renderingNoYes (runs synchronously)
Can you read/measure the DOMYes, but already after it is shown to the userYes, before it is shown (more accurate measurements)
Typical tasksRequests, subscriptions, logic that does not affect markupMeasuring and syncing the DOM, positioning, animations
How often it's usedVery oftenRarely (only when needed)

Example: measuring a DOM element

javascript
import { useLayoutEffect, useRef, useState } from "react"; function Box() { const boxRef = useRef(null); const [width, setWidth] = useState(0); useLayoutEffect(() => { // Runs before the browser paints const rect = boxRef.current.getBoundingClientRect(); setWidth(rect.width); }, []); return ( <div ref={boxRef} style={{ width: "50%" }}> Box width: {width}px </div> ); }

Here:

  • useLayoutEffect runs before the browser shows the page.
  • We measure the box width while the DOM is already ready, but not yet displayed.
  • This prevents "flashing" that would happen if the width were computed in useEffect.

Example: syncing scroll or positioning

javascript
function ScrollSync({ scrollTo }) { const ref = useRef(null); useLayoutEffect(() => { ref.current.scrollTop = scrollTo; // exactly before painting }, [scrollTo]); return ( <div ref={ref} style={{ height: 200, overflowY: "scroll", border: "1px solid gray" }} > <div style={{ height: 1000 }}>Content...</div> </div> ); }

If we used useEffect, the user might see the scroll "jump". With useLayoutEffect, everything happens before painting, without artifacts.


Important to remember

RuleExplanation
Runs synchronously after DOM changesBlocks painting until the code finishes
Use it only when you need to work with the DOM directlyFor example, measuring sizes, positioning
Do not use it for network requests or async actionsIt slows down page painting
You can have both useEffect and useLayoutEffect in the same componentReact itself decides the order (layout -> paint -> effect)

Final comparison

HookWhen it's calledWhat for
useEffectAfter the browser has shown the changesSide effects that do not affect the DOM
useLayoutEffectBefore the browser has shown the changesWorking with the DOM, measurements, syncing before painting

Summary

QuestionAnswer
What does useLayoutEffect() doRuns code right after the DOM updates, but before the browser paints the changes
When to use itWhen you need to measure or change the DOM before it is shown to the user
Why be carefulIt blocks painting and can hurt performance if used unnecessarily
ExamplesMeasuring sizes, scroll, animations, preventing content flashing

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.