Skip to content

QuizNot attempted

Render vs useLayoutEffect vs useEffect order

On mount, in what order do these three logs fire?
function App() {
console.log('render');
useEffect(() => {
console.log('useEffect');
});
useLayoutEffect(() => {
console.log('useLayoutEffect');
});
return <div>hi</div>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • render, useLayoutEffect, useEffect
  • Render runs first
  • then in the commit phase React applies DOM changes and synchronously runs layout effects before the browser paints
  • passive useEffects run after paint.
  • So: render, useLayoutEffect, useEffect.
  • Not useEffect before layout: passive effects are deferred.
  • Not layout before render: effects can't run before the component has rendered.
  • Not non-deterministic: the order is guaranteed.