Memberstack
Pricing
API
Login
Build for free
Memberstack
Pricing
API
Login
Build for free
Memberstack
Pricing
API
Login
Build for free
Blog/Product/useLayoutEffect vs useEffect - The Difference
Product#React

useLayoutEffect vs useEffect - The Difference

Sometimes we want to perform operations outside the scope of this data flow process, like interacting with an API. These operations are called side effects. In this article, we will explore how to handle side effects in React using a hook called useLayoutEffect.

Divine Orji·Contributor
|
March 23, 2022·4 min read

React’s data flow process is pretty straightforward. It breaks down a user interface into components, builds a tree of elements, and makes necessary updates to the DOM based on modifications in those components. However, sometimes we want to perform operations outside the scope of this data flow process, like interacting with an API. These operations are called side effects.

A “side effect” is anything that results in observable changes after a function has finished running, apart from the return value of the function itself.

Common side effects include network requests, data-fetching, and manual DOM mutations.

In this article, we will explore how to handle side effects in React using a hook called useLayoutEffect.

Prerequisites

To understand this article, we need the following:

  • Good knowledge of functional programming using JavaScript and React.

Experience with React useEffect.

What is useLayoutEffect?

According to React’s official documentation:

  • The signature is identical to useEffect, but it fires synchronously after all DOM mutations.

To understand this better, let’s take a look at useEffect's function signature:

javascript
useEffect(
 // callback function,
[ /* dependencies */ ]
);

useEffect takes in two arguments:

  • A callback function containing the side effect logic.
  • An array of dependencies that trigger the callback when updated between renders. If there are no dependencies, it stays as an empty array.

Let’s compare it to useLayoutEffect's function signature:

javascript
useLayoutEffect(
 // callback function,
[ /* dependencies */ ]
);

useLayoutEffect vs useEffect - the difference

As we’ve seen so far, both hooks are identical and, in most cases, can even be used interchangeably. But, there’s one key difference between them: execution time. Take a look at the diagrams below:

Blog image

In useEffect, when a user triggers a render (by updating state or props), React updates the screen, and the useEffect callback function runs asynchronously in the background.

‍

Blog image

In useLayoutEffect, the callback function runs synchronously before the screen is updated.

Let’s see an example that illustrates these concepts better. Take a look at the code below:

javascript
const Comparison = () => {
  // with useEffect
  useEffect(() => {
    console.log('This is useEffect');
  }, []);

  // with useLayoutEffect
  useLayoutEffect(() => {
    console.log('This is useLayoutEffect');
  }, []);

  return <div>Comparison</div>;
};

export default Comparison;

In the code above:

  • We created a component implementing the useEffect hook and the useLayoutEffect hook.
  • Each hook logs a string to the console to help us identify the hook type.

Let’s take a look at the console:

Blog image

‍

As we can see here, since useLayoutEffect runs synchronously before the DOM updates, its result will be ready for the console first.

After the DOM updates, useEffect will run asynchronously and display its results on the console.

When to use useLayoutEffect

In most cases, to make our app faster and more performant, it is recommended to handle side effects with useEffect since it executes after the DOM updates, preventing blocking issues on the browser.

However, when we need to do something based on the layout of our DOM and there are visual inconsistencies with useEffect, it is best to use useLayoutEffect.

For example, let’s create a button that will display a colored rectangle when clicked.

Here’s its implementation with useEffect:

javascript
import React, { useEffect, useRef, useState } from 'react';

const RenderRectangle = () => {
  const [display, setDisplay] = useState(false);
  const rectangle = useRef();
  
  useEffect(() => {
    if (rectangle.current == null) return;
    rectangle.current.style.backgroundColor = 'green';
    rectangle.current.style.marginTop = '20px';
  }, [display]);
  
  return (
    <div>
      <h1>useEffect - Render Rectangle</h1>
      <div>
        <button
          style={{ width: 100, height: 40, borderRadius: 5 }}
          onClick={() => setDisplay(!display)}
        >
          {display ? 'Hide' : 'Show'}
        </button>
        {/* Rectangle */}
        {display && (
          <div
            style={{ width: 100, height: 50, backgroundColor: 'red' }}
            ref={rectangle}
          ></div>
        )}
      </div>
    </div>
  );
};

export default RenderRectangle;

In this code:

  • We set the default state of the rectangle to false using useState.
  • We used useRef to refer to our rectangle div, allowing us to apply side effects on it using useEffect.
  • We wrote some markup for displaying the rectangle at the click of the button. The button will change display's state from true to false and vice versa.
  • We used useEffect to style the rectangle after render.

Here’s the result:

Blog image

‍

Notice that when the button is clicked, the rectangle displays with its initial background color of red, and quickly changes to green from the useEffect. The top margin of the rectangle also gets updated.

For a smoother effect, it will be better to use useLayoutEffect like this:

javascript
import React, { useLayoutEffect, useRef, useState } from 'react';

const RenderRectangle = () => {
  const [display, setDisplay] = useState(false);
  const rectangle = useRef();

  useLayoutEffect(() => {
    if (rectangle.current == null) return;
    rectangle.current.style.backgroundColor = 'green';
    rectangle.current.style.marginTop = '20px';
  }, [display]);

  return (
    <div style={{ width: '100%', maxWidth: '800px', margin: 'auto' }}>
      <h1>useLayoutEffect - Render Rectangle</h1>
      <div>
        <button
          style={{ width: 100, height: 40, borderRadius: 5 }}
          onClick={() => setDisplay(!display)}
        >
          {display ? 'Hide' : 'Show'}
        </button>
        {/* Rectangle */}
        {display && (
          <div
            style={{ width: 100, height: 50, backgroundColor: 'red' }}
            ref={rectangle}
          ></div>
        )}
      </div>
    </div>
  );
};

export default RenderRectangle;

Here’s the result:

Blog image

As we can see, there are no color or position glitches in this one.

Conclusion

In this article, we learned what useLayoutEffect is used for, when to use it, and how. For most use cases, it is a good idea to stick with useEffect, but feel free to use useLayoutEffect as the need arises.

To see the demos used in the article, check the CodeSandbox below:

https://codesandbox.io/embed/uselayouteffect-fenjtg?fontsize=14&hidenavigation=1&theme=dark

Topics

#React

Written by

Divine Orji

Contributor

Contents

  • Prerequisites
  • What is useLayoutEffect?
  • useLayoutEffect vs useEffect - the difference
  • When to use useLayoutEffect
  • Conclusion

Share

Explore Memberstack

  • All Features
  • Customer Showcase
  • Templates
  • Find Experts
  • Pricing
  • Help Center

Related Articles

Product

Subscription Business Pricing Models: How to price your Subscription Business with Webflow and Memberstack 

In this article, we’ll compare the various pricing models used in the industry. By understanding the various pricing models available, you can make an informed decision when choosing how you’ll build your subscription site and start generating revenue.

Shuib Abdullah

Mar 27, 2023

Product

Nine Free Webflow Subscription Templates

Subscriptions are the most direct way to engage with visitors to your site. Designing your SaaS, agency website, or blog to convert visitors to subscribers can help get your business off of the ground.

Robert Jett

Mar 23, 2023

Product

Job board pricing models: How to price your job board with Webflow and Memberstack 

In this article, we’ll compare the various pricing models used in the industry. By understanding the various pricing models available, you can make an informed decision when choosing how you’ll build your job board and start generating revenue.

Shuib Abdullah

Mar 3, 2023

Product

  • Full Feature List
  • User Accounts
  • Gated Content
  • Secure Payments
  • API & Integrations
  • Data Tables
  • Memberstack & Webflow
  • Memberstack & Claude Code
  • Memberstack & WordPress
  • Create a new account
  • 2.0 Log in
  • 1.0 Log in
  • Pricing

Learn about Memberstack

  • Showcase
  • Testimonials
  • Why Memberstack
  • Memberstack vs Outseta
  • Memberstack vs Memberspace
  • Memberstack vs Webflow Memberships

Company

  • About
  • Careers
  • Changelog
  • Partnerships(email)
  • Contact Us(email)
  • X
  • LinkedIn
  • "Do Not Sell My Data" Promise

Resources

  • Vibe Coding
  • Templates
  • Components
  • MemberScripts
  • Data Attributes
  • Find Experts
  • Hiring Guide
  • Webflow Slack Community
  • WordPress Slack Community
  • Support Forum
  • Help Center
  • Blog
  • llms.txt (for AI)
  • llms-full.txt (for AI)
AICPA
SOC

SOC 2 TYPE 1

CERTIFIED

GDPR

COMPLIANT

CCPA

COMPLIANT

Privacy PolicyTerms of ServiceCookie PolicySecurity Policy

© Memberstack Inc. 2018 - 2025. All rights reserved.

Memberstack - The no-code membership platform for any website | Product Hunt