Skip to content
react questions

React · 3 of 13senior~1 min

Explain the difference between useEffect and useLayoutEffect.

#hooks

useEffect vs useLayoutEffect

Execution order

text
Render -> DOM mutations -> useLayoutEffect -> Browser paints -> useEffect
useEffectuseLayoutEffect
WhenAfter paint (async)Before paint (sync)
Blocks browserNoYes
SSRDoesn't run on the serverDoesn't run on the server (the SSR warning was removed in React 19)

When to use useLayoutEffect

  • You need to measure DOM element dimensions (getBoundingClientRect).
  • You need to synchronously update the DOM to avoid visual flickering.
tsx
useLayoutEffect(() => { const { height } = ref.current.getBoundingClientRect(); setHeight(height); // before paint - user won't see a jump }, []);

Rule: use useEffect by default. Switch to useLayoutEffect only when you see visible visual artifacts.

updated

did you know it?

read more on the blog

view all

related questions