๐ŸŒ
Kent C. Dodds
kentcdodds.com โ€บ blog โ€บ useeffect-vs-uselayouteffect
useEffect vs useLayoutEffect
However, if your effect is mutating the DOM (via a DOM node ref) and the DOM mutation will change the appearance of the DOM node between the time that it is rendered and your effect mutates it, then you don't want to use useEffect. You'll want to use useLayoutEffect.
๐ŸŒ
React
react.dev โ€บ reference โ€บ react โ€บ useLayoutEffect
useLayoutEffect โ€“ React
All of this needs to happen before the browser repaints the screen. You donโ€™t want the user to see the tooltip moving. Call useLayoutEffect to perform the layout measurements before the browser repaints the screen:
Discussions

When to use useEffect or useLayoutEffect

I will go to my grave believing useEffect is one of the most abused and unecissary hooks a lot of the time.

I'm not saying it doesn't have it's place, but too often people are using it to change data on render, which just causes a new render. Instead they could just isolate the data change from react entirely (which makes sense given react is a view layer and not a full mvc) and have the first render be a cause of the change.

I can't count the number of times I've seen people have a useEffect that checks to see if a useState value changed and loads data based on it. It's like... Just load the data where the useState change was triggered!

More on reddit.com
๐ŸŒ r/reactjs
54
133
February 15, 2020
reactjs - When to use useImperativeHandle, useLayoutEffect, and useDebugValue - Stack Overflow
I cannot understand why the following useImperativeHandle, useLayoutEffect, and useDebugValue hooks are needed, can you give examples when they can be used, but not examples from the documentation ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
reactjs - Is `useLayoutEffect` preferable to `useEffect` when reading layout? - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... One difference about useLayoutEffect vs useEffect is that useLayoutEffect ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
React documentation on useLayoutEffect makes me confused
React renders -> useLayoutEffect -> browser renders (draws) -> useEffect More on reddit.com
๐ŸŒ r/reactjs
8
9
June 27, 2023
๐ŸŒ
Refine
refine.dev โ€บ home โ€บ blog โ€บ tutorials โ€บ a guide to using the uselayouteffect hook in react
A Guide to Using the useLayoutEffect Hook in React | Refine
August 12, 2024 - Unlike the useEffect hook, the useLayoutEffect hook runs synchronously which means it runs immediately after React has performed all the necessary DOM mutations but just before the browser paints the screen. It has the same API and possesses a similar syntax as the useEffect hook. This hook was introduced to solve some layout specific / niche issues that plagued devs when using the useEffect hook.
๐ŸŒ
Dave Ceddia
daveceddia.com โ€บ useeffect-vs-uselayouteffect
When to useLayoutEffect Instead of useEffect (example)
July 15, 2020 - Notice how the version with useLayoutEffect only updates visually once even though the component rendered twice. The useEffect version, on the other hand, visually renders twice, so you see a flicker where the value is briefly 0. Most of the time, useEffect is the right choice. If your code is causing flickering, switch to useLayoutEffect and see if that helps.
๐ŸŒ
DEV Community
dev.to โ€บ emmanuelthecoder โ€บ useeffect-vs-uselayouteffect-the-difference-and-when-to-use-them-124c
useEffect vs useLayoutEffect: the difference and when to use them - DEV Community
January 15, 2024 - This is because useLayoutEffect is fired synchronously after DOM is mutated and before the browser paints the new changes. Okay, I will like you to notice the change well in order to better understand how it works. Let's reference a DOM element with the useRef React hook and let's perform some changes with the two side effect hooks we've been talking about
๐ŸŒ
Medium
medium.com โ€บ @ignatovich.dm โ€บ deep-dive-into-uselayouteffect-in-react-with-examples-1e3b14da3d4f
Deep Dive into useLayoutEffect in React with Examples | by Frontend Highlights | Medium
October 30, 2024 - Reading DOM dimensions: When your componentโ€™s layout relies on precise measurements of the DOM, useLayoutEffect ensures that calculations are consistent with the rendered result. Synchronizing scrolling: If you need to scroll to a specific position or adjust layout as a result of user interaction, useLayoutEffect can ensure that changes are applied before the user sees the result.
๐ŸŒ
Fishtank
getfishtank.com โ€บ insights โ€บ understanding-the-differences-useeffect-vs-uselayouteffect
Understanding the Differences: useEffect vs useLayoutEffect | Fishtank
Essentially, useLayoutEffect is a version of useEffect that fires before the browser repaints the screen, making it ideal for cases where you need to measure or manipulate the DOM and ensure changes are made before the browser renders the next frame.
๐ŸŒ
Reddit
reddit.com โ€บ r/reactjs โ€บ when to use useeffect or uselayouteffect
r/reactjs on Reddit: When to use useEffect or useLayoutEffect
February 15, 2020 - There are exceptions to everything, but useEffect was originally designed to update elements not done by your components, or update based on calculated values. An example of the first is updating the page title based on a prop. An example of the second is calculating something based on the height of s tendered div (which you'd actually use useLayoutEffect for as this article so nicely points out)
Find elsewhere
๐ŸŒ
Medium
emrebener.medium.com โ€บ when-to-use-uselayouteffect-over-useeffect-in-react-e68bd2653282
When to Use useLayoutEffect Over useEffect in React | by Emrebener | Medium
January 31, 2026 - It is most commonly used when you need to measure or manipulate DOM elements prior to rendering to prevent visual inconsistencies. ... setup: The function with your Effectโ€™s logic.
๐ŸŒ
DEV Community
dev.to โ€บ nibble โ€บ what-is-uselayouteffect-hook-and-when-do-you-use-it-3lan
What is useLayoutEffect hook and when do you use it - DEV Community
October 25, 2021 - It should be noted that if a component ... useLayoutEffect hook is in the timing of their invocation. useEffect hook is invoked after the DOM has been painted....
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ react-hooks-when-to-use-uselayouteffect-instead-of-useeffect-3271a96d881a
React Hooks - When to Use useLayoutEffect Instead of useEffect | JavaScript in Plain English
July 3, 2023 - The gif below shows two chat boxes โ€” after the page loads, messages are โ€œfetchedโ€ and then each chat box scrolls to the bottom. The left box uses the useLayoutEffect hook to scroll to the bottom and the right one uses useEffect. You can notice some strange flashing only in the right box using useEffect.
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ react uselayouteffect vs. useeffect hooks with examples
React useLayoutEffect vs. useEffect Hooks with examples - LogRocket Blog
June 4, 2024 - It was designed to handle side ... it is highly encouraged in specific use cases, such as when measuring DOM elements, or animating or transitioning elements....
Top answer
1 of 5
246

Allow me to preface this answer by stating that all of these hooks are very rarely used. 99% of the time, you won't need these. They are only meant to cover some rare corner-case scenarios.


useImperativeHandle

Usually when you use useRef you are given the instance value of the component the ref is attached to. This allows you to interact with the DOM element directly.

useImperativeHandle is very similar, but it lets you do two things:

  1. It gives you control over the value that is returned. Instead of returning the instance element, you explicitly state what the return value will be (see snippet below).
  2. It allows you to replace native functions (such as blur, focus, etc) with functions of your own, thus allowing side-effects to the normal behavior, or a different behavior altogether. Though, you can call the function whatever you like.

There could be many reasons you want might to do either of the above; you might not want to expose native properties to the parent or maybe you want to change the behavior of a native function. There could be many reasons. However, useImperativeHandle is rarely used.

useImperativeHandle customizes the instance value that is exposed to parent components when using ref

Example

In this example, the value we'll get from the ref will only contain the function blur which we declared in our useImperativeHandle. It will not contain any other properties (I am logging the value to demonstrate this). The function itself is also "customized" to behave differently than what you'd normally expect. Here, it sets document.title and blurs the input when blur is invoked.

const MyInput = React.forwardRef((props, ref) => {
  const [val, setVal] = React.useState('');
  const inputRef = React.useRef();

  React.useImperativeHandle(ref, () => ({
    blur: () => {
      document.title = val;
      inputRef.current.blur();
    }
  }));

  return (
    <input
      ref={inputRef}
      val={val}
      onChange={e => setVal(e.target.value)}
      {...props}
    />
  );
});

const App = () => {
  const ref = React.useRef(null);
  const onBlur = () => {
    console.log(ref.current); // Only contains one property!
    ref.current.blur();
  };

  return <MyInput ref={ref} onBlur={onBlur} />;
};

ReactDOM.render(<App />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
<div id="app"></div>


useLayoutEffect

While similar to some extent to useEffect(), it differs in that it will run after React has committed updates to the DOM. Used in rare cases when you need to calculate the distance between elements after an update or do other post-update calculations / side-effects.

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

Example

Suppose you have an absolutely positioned element whose height might vary and you want to position another div beneath it. You could use getBoundingClientRect() to calculate the parent's height and top properties and then just apply those to the top property of the child.

Here you would want to use useLayoutEffect rather than useEffect. See why in the examples below:

With useEffect: (notice the jumpy behavior)

const Message = ({boxRef, children}) => {
  const msgRef = React.useRef(null);
  React.useEffect(() => {
    const rect = boxRef.current.getBoundingClientRect();
    msgRef.current.style.top = `${rect.height + rect.top}px`;
  }, []);

  return <span ref={msgRef} className="msg">{children}</span>;
};

const App = () => {
  const [show, setShow] = React.useState(false);
  const boxRef = React.useRef(null);

  return (
    <div>
      <div ref={boxRef} className="box" onClick={() => setShow(prev => !prev)}>Click me</div>
      {show && <Message boxRef={boxRef}>Foo bar baz</Message>}
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("app"));
.box {
  position: absolute;
  width: 100px;
  height: 100px;
  background: green;
  color: white;
}

.msg {
  position: relative;
  border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
<div id="app"></div>

With useLayoutEffect:

const Message = ({boxRef, children}) => {
  const msgRef = React.useRef(null);
  React.useLayoutEffect(() => {
    const rect = boxRef.current.getBoundingClientRect();
    msgRef.current.style.top = `${rect.height + rect.top}px`;
  }, []);

  return <span ref={msgRef} className="msg">{children}</span>;
};

const App = () => {
  const [show, setShow] = React.useState(false);
  const boxRef = React.useRef(null);

  return (
    <div>
      <div ref={boxRef} className="box" onClick={() => setShow(prev => !prev)}>Click me</div>
      {show && <Message boxRef={boxRef}>Foo bar baz</Message>}
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("app"));
.box {
  position: absolute;
  width: 100px;
  height: 100px;
  background: green;
  color: white;
}

.msg {
  position: relative;
  border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
<div id="app"></div>


useDebugValue

Sometimes you might want to debug certain values or properties, but doing so might require expensive operations which might impact performance.

useDebugValue is only called when the React DevTools are open and the related hook is inspected, preventing any impact on performance.

useDebugValue can be used to display a label for custom hooks in React DevTools.

I have personally never used this hook though. Maybe someone in the comments can give some insight with a good example.

2 of 5
22

useImperativeHandle

useImperativeHandle allows you to determine which properties will be exposed on a ref. In the example below, we have a button component, and we'd like to expose the someExposedProperty property on that ref:

[index.tsx]

import React, { useRef } from "react";
import { render } from "react-dom";
import Button from "./Button";

import "./styles.css";

function App() {
  const buttonRef = useRef(null);

  const handleClick = () => {
    console.log(Object.keys(buttonRef.current)); // ['someExposedProperty']
    console.log("click in index.tsx");
    buttonRef.current.someExposedProperty();
  };

  return (
    <div>
      <Button onClick={handleClick} ref={buttonRef} />
    </div>
  );
}

const rootElement = document.getElementById("root");
render(<App />, rootElement);

[Button.tsx]

import React, { useRef, useImperativeHandle, forwardRef } from "react";

function Button(props, ref) {
  const buttonRef = useRef();
  useImperativeHandle(ref, () => ({
    someExposedProperty: () => {
      console.log(`we're inside the exposed property function!`);
    }
  }));
  return (
    <button ref={buttonRef} {...props}>
      Button
    </button>
  );
}

export default forwardRef(Button);

Available here.

useLayoutEffect

This is the same as useEffect, but only fires once all DOM mutations are completed. This article From Kent C. Dodds explains the difference as well as anyone, regarding these two, he says:

99% of the time [useEffect] is what you want to use.

I haven't seen any examples which illustrate this particularly well, and I'm not sure I'd be able to create anything either. It's probably best to say that you ought to only use useLayoutEffect when useEffect has issues.

useDebugValue

I feel like the docs do a pretty good example of explaining this one. If you have a custom hook, and you'd like to label it within React DevTools, then this is what you use.

If you have any specific issues with this then it'd probably be best to either comment or ask another question, because I feel like anything people put here will just be reiterating the docs, at least until we reach a more specific problem.

๐ŸŒ
Telerik
telerik.com โ€บ blogs โ€บ uselayouteffect-powerful-hook
useLayoutEffect Is a Powerful Hook
May 17, 2022 - We can see that before our screen is updated, the name state is updated. The useLayoutEffect runs and updates the name state before the browser has a chance to paint. Most of the time the useEffect hook is enough and it will serve us perfectly.
๐ŸŒ
Medium
tech.groww.in โ€บ useeffect-vs-uselayouteffect-in-plain-language-33eb1c7c1f87
useEffect vs. useLayoutEffect in plain language | by Aakash Sangwan | The Groww Engineering Blog
April 21, 2025 - The difference comes from where useLayoutEffect will be called before the user can see the visual changes in that render whereas useEffect will be called after a user is able to see the visual changes in that render.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ reactjs-uselayouteffect-hook
ReactJS useLayoutEffect Hook | GeeksforGeeks
November 2, 2023 - But to prevent blocking the page loading, we should always use the useEffect hook. ... The useLayoutEffect hook works in the same phase as componentDidMount and componentDidUpdate methods.
๐ŸŒ
Medium
medium.com โ€บ @revivecoding โ€บ react-hooks-when-to-use-uselayouteffect-instead-of-useeffect-9ad8fa4c048b
React Hooks โ€” When to Use useLayoutEffect Instead of useEffect | by Revive Coding | Medium
December 6, 2022 - Lets under this in more simple words with example: The useLayoutEffect hook is used to perform side effects that need to be executed synchronously immediately after React has performed the layout and painted the components on the screen.
๐ŸŒ
Upmostly
upmostly.com โ€บ home โ€บ tutorials โ€บ when should you use the uselayouteffect hook in react?
When should you use the useLayoutEffect hook in React? - Upmostly
September 8, 2022 - That means that in case we do need to make any JavaScript queries for any DOM elements, useLayoutEffect is the hook we might need. You cause a render somehow (change state, or the parent re-renders) ...
๐ŸŒ
Alex MacArthur
macarthur.me โ€บ posts โ€บ when-i-needed-uselayouteffect-in-react
When I Actually Needed useLayoutEffect() in React | Alex MacArthur
March 4, 2023 - If youโ€™ve worked with React for any length of time, youโ€™ve probably heard of the infamous useLayoutEffect() hook. Youโ€™ve also probably never used it. Thatโ€™s because for most cases, useEffect() covers the vast majority of use cases with a much lower chance of shooting yourself in the foot. In fact, the the React docs even explicitly encourage you to use the latter whenever possible to avoid performance gotchas.