• _hello
  • _about-me
  • _projects
  • _blog
  • _support
_contact-me
find me in:
@faeztgh
cd ../blog~/blog/useeffect-vs-uselayouteffect
  • React
  • Hooks
  • useEffect
  • useLayoutEffect

useEffect vs useLayoutEffect

Learn the key differences between useEffect and useLayoutEffect in React — when each hook fires, how they handle DOM mutations, and which one to reach for.

FFaez Tgh·Feb 20, 20241 min read·291 words
// share: post linkedin
Side-by-side comparison of useEffect and useLayoutEffect timing in React's render cycle

In this article, we'll dive into the specifics of two important hooks in React: useEffect and useLayoutEffect. We will learn about their usage, differences, and how to choose between them for different scenarios.

What is useEffect?#

The useEffect hook lets you perform side effects in functional components. Side effects refer to any operation that interacts with the outside environment, such as data fetching, subscriptions, timers, logging, and manual DOM manipulations.

Syntax:

ts
useEffect(didUpdate [inputs]);

Example:

tsx
import React, { useState, useEffect } from 'react';

function Example() {
  const [count, setCount] = useState(0);
  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    document.title = `You clicked ${count} times`;
  });
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

In the example above, after every render, the function provided to useEffect updates the document title.

What is useLayoutEffect?#

The useLayoutEffect is almost identical to useEffect but it fires synchronously after all DOM mutations. Use this to read the layout from the DOM and synchronously re-render. Updates scheduled inside useLayoutEffect will be flushed synchronously before the browser has a chance to paint.

Syntax:

ts
useLayoutEffect(didUpdate [inputs]);

Example:

tsx
import React, { useLayoutEffect } from 'react';

function Example() {
  useLayoutEffect(() => {
    // Manipulate the DOM in here
  }, []);
  return <div>Hello, world!</div>;
}

Differences Between useEffect and useLayoutEffect#

  1. Execution Timing: useEffect runs asynchronously and after a render is painted to the screen. useLayoutEffect, on the other hand, runs synchronously immediately after React has performed all DOM mutations. This means if you're making DOM changes, you could cause a synchronous re-render by modifying the state.
  2. Visual Effects: If causing a visual update, use useLayoutEffect to avoid flickering. When using useEffect, components get rendered first with DOM mutations and get updated later when useEffect is called.
  3. Server Rendering: useEffect does not run on server rendering while useLayoutEffect does. However, this results in a warning, since the effect tries to update after the component has already been sent to client.

Ultimately, the rule of thumb is to prefer useEffect unless there is a compelling reason or specific scenario that requires useLayoutEffect.

That's it! You're now equipped with a solid understanding of useEffect and useLayoutEffect. Happy coding!

References:

  • React Hooks API Reference
  • A Complete Guide to useEffect

#on this page

  • What is useEffect?
  • What is useLayoutEffect?
  • Differences Between useEffect and useLayoutEffect
Nextjs Best Optimization Tools olderYouTube Playback Controller: A Powerful Chrome Extension for Video Speed Controlnewer

# related-posts

3 more
  • 01
    What's New in Next.js 16Jul 15, 2026·5 min
  • 02
    Server vs Client Components: A Practical Mental ModelJul 1, 2026·4 min
  • 03
    Streaming AI Chat in Next.js with the AI SDKJun 14, 2026·5 min