I'm guessing the only difference is that useEffect will guarantee that it will always run after the render, it also makes sure that every other component that needs to re-render will finish rendering before running the effect. Though I can't really think of a good use case for this. Answer from ImprovementReady4791 on reddit.com
🌐
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.

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>

Discussions

[Compiler Bug]: `useLayoutEffect` without dependency array should be allowed by either `react-hooks/exhaustive-deps` or `react-compiler/react-compiler`
So... either you should fix the react-hooks/exhaustive-deps rule to allow effects without dependencies (useEffect seem to allow this, but not useLayoutEffect for some reason), or the compiler should be smarter about what disabled ESLint rules to take into account... More on github.com
🌐 github.com
5
October 24, 2024
Why does eslint warn about useEffect having a missing dependency? - react - ReScript Forum
Some form of analysis might be able to help with this. Hard to say precisely without fully understanding the problem. To aid understanding: is this a react-specific idiosyncrasy or is it common to all UI frameworks? More on forum.rescript-lang.org
🌐 forum.rescript-lang.org
July 13, 2022
Bug: Clean up useEffect given [] empty dependency array causes a tedious linter error
Below is an example component (full component LINK) which is causing a linter error if not disabled. This issue propbably relates to #16265 ;-) A clean up useEffect given an empty dependency array, using a props method causes an error Re... More on github.com
🌐 github.com
11
September 4, 2023
"React Hook useEffect has a missing dependency"
im getting this error. why is it happening and how can i solve this? React Hook useEffect has a missing dependency: 'getRecipes'. Either include it or remove the dependency array react-hooks/exhaustive-deps and this … More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
July 23, 2020
🌐
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.
🌐
DEV Community
dev.to β€Ί heyitsaastha β€Ί 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.
🌐
React
react.dev β€Ί learn β€Ί removing-effect-dependencies
Removing Effect Dependencies – React
To remove a dependency, you need to β€œprove” to the linter that it’s not necessary. If some code should run in response to a specific interaction, move that code to an event handler. If different parts of your Effect should re-run for different reasons, split it into several Effects. If you want to update some state based on the previous state, pass an updater function. If you want to read the latest value without β€œreacting” it, extract an Effect Event from your Effect.
🌐
GitHub
github.com β€Ί facebook β€Ί react β€Ί issues β€Ί 31341
[Compiler Bug]: `useLayoutEffect` without dependency array should be allowed by either `react-hooks/exhaustive-deps` or `react-compiler/react-compiler` Β· Issue #31341 Β· facebook/react
October 24, 2024 - This will generate a react-hooks/exhaustive-deps warning for using useLayoutEffect without dependencies, but the dependency array is optional when you want the effect to run on every render, which I do in this case.
Author Β  Svish
Find elsewhere
🌐
GreatFrontEnd
greatfrontend.com β€Ί questions β€Ί quiz β€Ί what-does-the-dependency-array-of-useeffect-affect
What does the dependency array of `useEffect` affect? | Quiz Interview Questions with Solutions
September 5, 2021 - If you use state or props inside the effect without including them in the dependency array, you might end up with stale values.
🌐
Epic React
epicreact.dev β€Ί why-you-shouldnt-put-refs-in-a-dependency-array
Why you shouldn't put refs in a dependency array | Epic React by Kent C. Dodds
July 9, 2024 - React Hook React.useEffect has a missing dependency: 'cbRef'. Either include it or remove the dependency array.
🌐
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 ...
🌐
ReScript Forum
forum.rescript-lang.org β€Ί t β€Ί why-does-eslint-warn-about-useeffect-having-a-missing-dependency β€Ί 3534
Why does eslint warn about useEffect having a missing dependency? - react - ReScript Forum
July 13, 2022 - Some form of analysis might be able to help with this. Hard to say precisely without fully understanding the problem. To aid understanding: is this a react-specific idiosyncrasy or is it common to all UI frameworks?
🌐
Medium
medium.com β€Ί @yhimanshu22 β€Ί 1-what-happens-if-useeffect-has-an-empty-dependency-array-bd5a450eb60c
1. What happens if useEffect has an empty dependency array? | by Himanshu Yadav | Medium
November 12, 2024 - Question: What is the behavior when useEffect(() => { /* effect */ }, []) is used? Answer: The effect runs only once, after the initial render. This is similar to componentDidMount() in class components.
🌐
CodeSandbox
codesandbox.io β€Ί s β€Ί useeffect-no-dependency-array-6pzs78
useEffect- no dependency array - CodeSandbox
June 18, 2023 - useEffect- no dependency array by Rick799 using loader-utils, react, react-dom, react-scripts
Published Β  Jun 12, 2023
Author Β  Rick799
🌐
W3Schools
w3schools.com β€Ί react β€Ί react_useeffect.asp
React useEffect
There are several ways to control when side effects run. We should always include the second parameter which accepts an array. We can optionally pass dependencies to useEffect in this array.
🌐
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
🌐
GitHub
github.com β€Ί facebook β€Ί react β€Ί issues β€Ί 27333
Bug: Clean up useEffect given [] empty dependency array ...
September 4, 2023 - A clean up useEffect given an empty dependency array, using a props method causes an error React Hook useEffect has a missing dependency: 'props'.
Author Β  myCatsName
🌐
Perficient Blogs
blogs.perficient.com β€Ί 2024 β€Ί 12 β€Ί 16 β€Ί avoiding infinite loops when utilizing useeffect() in reactjs
Avoiding Infinite Loops When Utilizing useEffect() in ReactJS / Blogs / Perficient
December 16, 2024 - This cycle repeats indefinitely, leading to an infinite loop of updating the count state and re-rendering the component. This happens because there is no dependency array, meaning the useEffect runs after every re-render of the component.
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί t β€Ί react-hook-useeffect-has-a-missing-dependency β€Ί 411332
"React Hook useEffect has a missing dependency" - The freeCodeCamp Forum
July 23, 2020 - im getting this error. why is it happening and how can i solve this? React Hook useEffect has a missing dependency: 'getRecipes'. Either include it or remove the dependency array react-hooks/exhaustive-deps and this …
🌐
Newline
newline.co β€Ί @RichardBray β€Ί useeffect-in-react-best-practices-and-common-pitfalls--52b2d5d7
useEffect in React: Best Practices and Common Pitfalls | newline
December 9, 2024 - In React development, the useEffect hook is a fundamental tool that allows you to manage side effects in function components. Side effects in React include operations like fetching data, subscribing to external data sources, manually modifying the DOM, and even handling timers.