If you only want to run the function given to useEffect after the initial render, you can give it an empty array as second argument.

function MyComponent() {
  useEffect(() => {
    loadDataOnlyOnce();
  }, []);

  return <div> {/* ... */} </div>;
}
Answer from Tholle on Stack Overflow
🌐
Robin Wieruch
robinwieruch.de β€Ί react-useeffect-only-once
React useEffect only Once - Robin Wieruch
November 7, 2020 - import * as React from 'react'; const App = () => { const [toggle, setToggle] = React.useState(true); const handleToggle = () => { setToggle(!toggle); }; const calledOnce = React.useRef(false); React.useEffect(() => { if (calledOnce.current) { return; } if (toggle === false) { console.log('I run only once if toggle is false.'); calledOnce.current = true; } }, [toggle]); return ( <div> <button type="button" onClick={handleToggle}> Toggle </button> {toggle && <div>Hello React</div>} </div> ); }; export default App;
Discussions

What is the best way to execute a piece of code just once when the component mounts?
I’ve always gone with option 1 and frequently seen that as the suggested solution. Others smarter than me are welcome to chime in if that goes against best practices. More on reddit.com
🌐 r/reactjs
67
25
January 26, 2024
How to make a component render only AFTER a useEffect hook?
The `useEffect` hook runs after render so what you are asking for is not possible. The component will always render twice if you update your state in `useEffect`. You can show a loading state until you either have names successfully or you have encountered an error. As written above, your component has several errors in it. More on reddit.com
🌐 r/reactjs
13
0
January 26, 2023
How to prevent a React useEffect from running twice in StrictMode development with an external system that has no cleanup method?
React or not, a library that adds event handlers without a cleanup function is pretty bad. It's a memory leak and should be fixed upstream. More on reddit.com
🌐 r/reactjs
23
0
August 20, 2024
[reactjs] .map() function inside of a useEffect only runs once?
Function calls use a "snapshot" of your state when they are called. booksRead is 0 and remains 0 for each iteration of your map call. I would recommend summing up your booksRead and then calling setBooksRead with the final tally. More on reddit.com
🌐 r/reactjs
9
1
November 22, 2022
People also ask

How can you skip the useEffect execution on the first render?
Skipping useEffect on the first render can be achieved by employing strategies like memoizing the effect function with the useCallback hook, ensuring it runs only on subsequent renders.
🌐
dhiwise.com
dhiwise.com β€Ί post β€Ί how-to-make-useeffect-run-only-once-for-optimal
How to Make useEffect Run Only Once for React
How does the dependency array affect the useEffect hook's execution?
The dependency array controls when the effect is re-invoked; React will only re-run the effect if any value in the dependency array changes, allowing for precise control over component side effects.
🌐
dhiwise.com
dhiwise.com β€Ί post β€Ί how-to-make-useeffect-run-only-once-for-optimal
How to Make useEffect Run Only Once for React
What is the significance of the cleanup function in useEffect?
The cleanup function prevents memory leaks and ensures that components unmount cleanly by cleaning up resources or subscriptions created by the effect.
🌐
dhiwise.com
dhiwise.com β€Ί post β€Ί how-to-make-useeffect-run-only-once-for-optimal
How to Make useEffect Run Only Once for React
🌐
CSS-Tricks
css-tricks.com β€Ί run-useeffect-only-once
Run useEffect Only Once | CSS-Tricks
July 30, 2019 - import React, { useEffect } from 'react'; function App() { useEffect(() => { // Run! Like go get some data from an API. }); return ( <div> {/* Do something with data. */} </div> ); } In a totally isolated example like that, it’s likely the useEffect will run only once.
🌐
Javascriptf1
javascriptf1.com β€Ί how-to β€Ί run-useeffect-only-once-in-react
How to run useEffect only once in React - JavaScriptF1.com
November 13, 2020 - This second parameter is an array where you can define props and states your effect should depend on. If the array is empty then react only execute your code at the time of first rendering. useEffect(() => { // Initialization code comes here }, []);
🌐
DhiWise
dhiwise.com β€Ί post β€Ί how-to-make-useeffect-run-only-once-for-optimal
How to Make useEffect Run Only Once for React
October 25, 2024 - Passing an empty array ensures the effect runs only once during the initial render cycle. This technique effectively tells React to skip re-invoking the effect after the initial render, thus optimizing performance for component mounts.
🌐
Medium
medium.com β€Ί @reactcompany01 β€Ί how-to-call-loading-function-with-react-useeffect-only-once-e5fdbd598ec8
How to Call Loading Function with React UseEffect Only Once | by Reactcompany | Medium
March 27, 2023 - The callback will call after the initial render only. React hook makes a change in the passed-in function. The basic syntax of useEffect like ... Hook acquires two arguments: the first argument passed to useEffect is a function, and the second argument is an array of dependency. ... The function runs once following component mount for a pattern that validates react hook practice.
Find elsewhere
🌐
React
legacy.reactjs.org β€Ί docs β€Ί hooks-effect.html
Using the Effect Hook – React
If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) as a second argument. This tells React that your effect doesn’t depend on any values from props or state, so it never needs to re-run.
🌐
DEV Community
dev.to β€Ί esedev β€Ί useeffect-vs-useeffectonce-2cnk
useEffect vs useEffectOnce - DEV Community
March 8, 2023 - As you can see, useEffectOnce accepts a single argument, which is the callback function that will be executed once. It then calls useEffect with the same callback and an empty array as the second argument, which tells React to only execute the ...
🌐
Reddit
reddit.com β€Ί r/reactjs β€Ί what is the best way to execute a piece of code just once when the component mounts?
r/reactjs on Reddit: What is the best way to execute a piece of code just once when the component mounts?
January 26, 2024 -

Lets take an example - I want to initialize the SDK of a partner who will be loading an iframe on my page

  1. I can use a `useEffect` with empty dependency array. It will work fine in production but will be fired twice in development mode. And that does not feel right

  2. I can use `useRef` and base my logic around it, But this does not look intuitive either.

What would be the best way to do this?

🌐
Sink In
gosink.in β€Ί react-js-how-to-render-useeffect-only-once
React.js β€” How to execute useEffect hook only once?
May 24, 2020 - In this code, the code inside useEffect will only get executed once even if we change the value of the variable count multiple times. It'd print "Hello from the component!" twice but it'd print "Hello from useEffect" only once.
🌐
npm
npmjs.com β€Ί package β€Ί @custom-react-hooks β€Ί use-effect-once
@custom-react-hooks/use-effect-once - npm
August 18, 2024 - Initial Setup: Perform setup operations like fetching initial data or setting up listeners. One-time Calculations: Compute values needed only once during the component's lifecycle.
      Β» npm install @custom-react-hooks/use-effect-once
    
Published Β  Aug 18, 2024
Version Β  1.5.1
Author Β  Bane Grozdanovic
🌐
Sean C Davis
seancdavis.com β€Ί posts β€Ί run-react-effect-hook-only-once-in-strict-mode
Run React Effect Hook only Once in Strict Mode | Sean C Davis
To ensure this hook only runs once in development mode, we can add another reference object that tracks whether the callback to useEffect has been called. Something like this: import { useEffect, useState, useRef } from "react"; import styles ...
🌐
React
react.dev β€Ί reference β€Ί react β€Ί useEffect
useEffect – React
useEffect is a Hook, so you can only call it at the top level of your component or your own Hooks. You can’t call it inside loops or conditions.
🌐
30 Seconds of Code
30secondsofcode.org β€Ί home β€Ί react β€Ί hooks β€Ί useeffectonce hook
React useEffectOnce hook - 30 seconds of code
June 26, 2024 - The custom hook will then run the callback at most once when the condition becomes true the first time. const useEffectOnce = (callback, when) => { const hasRunOnce = React.useRef(false); React.useEffect(() => { if (when && !hasRunOnce.current) ...
🌐
LogRocket
blog.logrocket.com β€Ί home β€Ί how to use the useeffect hook in react: a complete guide
How to use the useEffect hook in React: A complete guide - LogRocket Blog
June 3, 2025 - That’s why using an empty dependency array makes React invoke an effect only once β€” after the first render. The second render along with the second useEffect title is due to the state change invoked by setTitle() after we read the value ...
🌐
Greenroots
blog.greenroots.info β€Ί react-useeffect-hook-usages-you-must-know
React useEffect Hook usages you must know
March 22, 2022 - You can pass an empty array as the second argument to the useEffect hook to tackle this use case. ... In this case, the side effect runs only once after the initial render of the component.
🌐
usehooks-ts
usehooks-ts.com β€Ί react-hook β€Ί use-effect-once
useEffectOnce
useSsr: It was not a React hook. useEffectOnce: Unnecessary abstraction, prefer built-in React hooks. useUpdateEffect: Unnecessary abstraction, prefer built-in React hooks. useImageOnLoad: Too opinionated.
🌐
DEV Community
dev.to β€Ί cassidoo β€Ί when-useeffect-runs-3pf3
When useEffect runs - DEV Community
June 4, 2024 - If the dependency array is empty, useEffect is only called once (note: this has changed in React 18 in development and strict mode because of Suspense things, but this is how it is for Preact and pre-React 18 and I will talk about a workaround ...
🌐
Reddit
reddit.com β€Ί r/reactjs β€Ί how to make a component render only after a useeffect hook?
r/reactjs on Reddit: How to make a component render only AFTER a useEffect hook?
January 26, 2023 -

I have a component with a useEffect to fetch some data and it conditionally renders different children based on the response. So something like this

function Component() {
    const [names, setNames] = useState([])

    useEffect(() => {
        getNames().then(data => names = data)
    }, [names])

    return (
        <div>
            {names.length ? <Success /> <Error />}
        </div>
    )

Upon first render, it shows the Error component, and right after a split second, it shows the Success component. I want it to only render once the useEffect is finished. Is there anyway to do that?