It's not quite the same.

  • Giving it an empty array acts like componentDidMount as in, it only runs once.

  • Giving it no second argument acts as both componentDidMount and componentDidUpdate, as in it runs first on mount and then on every re-render.

  • Giving it an array as second argument with any value inside, eg , [variable1] will only execute the code inside your useEffect hook ONCE on mount, as well as whenever that particular variable (variable1) changes.

You can read more about the second argument as well as more on how hooks actually work on the official docs at https://reactjs.org/docs/hooks-effect.html

Answer from bamtheboozle on Stack Overflow
🌐
Reddit
reddit.com β€Ί r/reactjs β€Ί what is the purpose of useeffect without a dependency array?
r/reactjs on Reddit: What is the purpose of useEffect without a dependency array?
March 3, 2022 -

I use useEffect all the time in my applications, but I've never understood the difference between having code inside a useEffect with no dependency array, and just running it in the component itself.

From the documentation. What is the difference between this

function Example() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    document.title = `You clicked ${count} times`;
  });
}

and this?

function Example() {
  const [count, setCount] = useState(0);
  document.title = `You clicked ${count} times`;
}

wont both run on every render?

Edit: thanks for all the helpful responses. It seems like my intuition was almost right. It does behave slightly differently, but almost never in a way you want. However it would be good for protecting DOM manipulating functions for SSR.

🌐
DEV Community
dev.to β€Ί csituma β€Ί why-we-use-empty-array-with-useeffect-iok
Why we use empty array with UseEffect - DEV Community
December 10, 2022 - If you do not want the useEffect callback to run on every render, you should pass an empty array as an argument to useEffect. This tells React that the effect does not depend on any values from the component's props or state, so it only needs ...
🌐
GitHub
github.com β€Ί facebook β€Ί react β€Ί issues β€Ί 27333
Bug: Clean up useEffect given [] empty dependency array causes a tedious linter error Β· Issue #27333 Β· facebook/react
September 4, 2023 - IMO in 2023 the React linter causes ... both worked and caused no issues. The clean up useEffect has a different behavior when given no dependency array, an empty [] array, or a filled one....
Author Β  myCatsName
🌐
React
react.dev β€Ί reference β€Ί react β€Ί useEffect
useEffect – React
If you omit this argument, your Effect will re-run after every commit of the component. See the difference between passing an array of dependencies, an empty array, and no dependencies at all.
🌐
GitHub
github.com β€Ί facebook β€Ί react β€Ί issues β€Ί 20530
Bug: useEffect with no dependencies always fires a warning Β· Issue #20530 Β· facebook/react
January 1, 2021 - Use useEffect with no dependencies. ... Either no warning is fired for an intentional empty array, or an alternative hook useOnDidMount is added, which is useEffect but without dependencies.
Author Β  miquelvir
🌐
Medium
medium.com β€Ί devil-is-in-the-details β€Ί dependency-array-in-useeffect-hook-d73e0ef2ab33
Dependency array in useEffect hook | by Shreejit Rajbanshi | Devil is in the Details | Medium
November 20, 2022 - Any unrelated state change will ... Runs in each re-render of the component }) An empty array simply means that there are no dependencies that will trigger the callback within it....
Find elsewhere
Top answer
1 of 1
65

For this exact case you're right because undefined is passed as the dependencies of useEffect.

This means useEffect runs on every render and thus the event handlers will unnecessarily get detached and reattached on each render.

function listener() {
  console.log('click');
}

function Example() {
  const [count, setCount] = window.React.useState(0);

  window.React.useEffect(() => {

    console.log(`adding listener ${count}`);
    window.addEventListener("click", listener);

    return () => {
      console.log(`removing listener ${count}`);
      window.removeEventListener("click", listener);
    };
  }); // <-- because we're not passing anything here, we have an effect on each render
  
  window.React.useEffect(() => {
    setTimeout(() => {
      setCount(count + 1);
    }, 1000)
  });
  
  return count;
}

window.ReactDOM.render(window.React.createElement(Example), document.getElementById('root'))
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>

But if you explicitly declare no dependencies by passing in an empty array [], useEffect will only run once, thus making this pattern perfectly legitimate for event handler attachment.

function listener() {
  console.log('click');
}

function Example() {
  const [count, setCount] = window.React.useState(0);

  window.React.useEffect(() => {

    console.log(`adding listener ${count}`);
    window.addEventListener("click", listener);

    return () => {
      console.log(`removing listener ${count}`);
      window.removeEventListener("click", listener);
    };
  }, []); // <-- we can control for this effect to run only once during the lifetime of this component
  
  window.React.useEffect(() => {
    setTimeout(() => {
      setCount(count + 1);
    }, 1000)
  });
  
  return count;
}

window.ReactDOM.render(window.React.createElement(Example), document.getElementById('root'))
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>

🌐
John Kavanagh
johnkavanagh.co.uk β€Ί home β€Ί articles β€Ί why use empty dependency array in react's useeffect
Why Use Empty Dependency Array in React's useEffect, by John Kavanagh
October 28, 2025 - As you might have gathered, instead of passing an empty array, you can pass specific dependencies into the array instead. Then, React will only re‑run the effect when one of those dependencies changes.
🌐
Gillesferrand
gillesferrand.com β€Ί understanding-useeffect-no-dependencies-vs-empty-array-vs-dependencies-array
useEffect Hook: No, Empty, vs Dependencies Arrays
August 12, 2025 - Learn React's useEffect hook: behaviors with no dependencies, empty array, and dependencies. Understand their impacts, use cases, and application
🌐
DEV Community
dev.to β€Ί aasthapandey β€Ί useeffect-in-react-3flb
useEffect with and without dependency array in react - DEV Community
May 26, 2021 - As the useEffect does not have dependencies the useEffect get triggered when the page load. ... If we don't put a dependency array, useEffect() will be called on every render.
🌐
YouTube
youtube.com β€Ί watch
Why Use useEffect with Empty Dependency Array in React | #TechIn2 - YouTube
Welcome to another quick tutorial on Tech in 2! Today, we're talking about why we use the useEffect hook with an empty dependency array in React.πŸš€ **Why Emp...
Published Β  December 15, 2023
🌐
DhiWise
dhiwise.com β€Ί post β€Ί understanding-the-importance-of-the-useeffect-dependency-array-in-react
UseEffect Dependency Array
August 13, 2025 - This helps you visualize how React decides whether to run the useEffect callback function. Understanding this flow makes it easier to avoid unnecessary re executions. The React hooks exhaustive deps ESLint rule warns when your effect uses variables not listed in the dependency array.
🌐
Medium
medium.com β€Ί @kavishkanimsara9919 β€Ί understanding-useeffects-dependency-array-in-react-a-complete-guide-c9c2f71059fb
Understanding useEffect’s Dependency Array in React: A Complete Guide | by Kavishka Nimsara | Medium
February 6, 2025 - βœ”useEffect(fn) (no array) β†’ Runs on every render (⚠ avoid overuse!). Mastering these patterns will help you write better, more optimized React applications. Hope this guide helped you understand how to use the useEffect dependency array effectively! Have any questions?
🌐
Reddit
reddit.com β€Ί r/reactjs β€Ί can someone explain useeffect dependency arrays like im 5?
r/reactjs on Reddit: Can someone explain useEffect dependency arrays like im 5?
April 24, 2022 -

When do you need to use it? Why do I need to put a empty array even though its empty? When would I put something in the empty array?

Sorry if stupid noob question.

🌐
Afrie Irham
blog.afrieirham.com β€Ί understanding-useeffect-and-dependencies-array-in-react
Understanding useEffect and dependencies array in React
May 24, 2023 - So useEffect with no dependency arrays will run on every render. ... Now let's comment the first useEffect and uncomment the second one. Notice how the number is now 11 instead of infinitely getting bigger.
🌐
Codedamn
codedamn.com β€Ί news β€Ί react js
useEffect dependency array in React.js – Complete Guide
June 2, 2023 - These changes then trigger the callback function. The most basic dependency array would be an empty array. The empty array indicates that the useEffect doesn’t have any dependencies on any state variables.
🌐
Retool
retool.com β€Ί blog β€Ί hooks-and-state-102-the-dependency-array-in-useeffect
Hooks and state 102: the Dependency array in useEffect()
July 9, 2025 - Only if one of the dependencies has changed since the last render, will the effect be rerun. If you specify an empty array, then the effect will only be run once, upon component mount.