Here are the three scenarios: 1- No dependency array >> code inside useEffect runs every time your component re-renders. 2- Empty dependency array >> code inside your useEffect only runs once when your component first mounts. 3- Not-empty dependency array >> code inside your useEffect runs every time any variables you put inside the dependency array change. The last part isn't exactly accurate. You components also needs to re-render. so if your put a state or a prob inside your dependency array, your component will re-render and the useEffect will run. However, if you put some kind of a const or locally defend variable that doesn't trigger a re-render when it's changed, the useEffect will only fire again with the next re-render. Answer from ritwal on reddit.com
🌐
Medium
medium.com › suyeonme › react-lets-deep-dive-into-deps-array-of-useeffect-13ab96468db7
React: Let’s deep dive into deps array of useEffect | by Suyeon Kang | suyeonme | Medium
July 31, 2022 - If you don’t specify the dependency array, it will run in every render. useEffect hook provides two types.
🌐
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.

Discussions

Array in useEffect dependency array

Are you mutating the array? Can you post the code that you’re using to change the array itself?

More on reddit.com
🌐 r/reactjs
4
5
August 13, 2019
Adding an array in the useEffect dependency array causes an infinte loop
yes, so this would be the expected behavior. Any time the contents of that dependency array change, that first argument, the callback function, will be run again. So, you are setting tasks in the API call, which changes the contents of that dependency array, which calls that callback again, which sets state again, in an infinite loop. There are two solutions for you here. Either, remove tasks from that dependency array, which violates the "exhaustive deps" rule, whereby the react team strongly recommends having all dependencies of that function in the dependency array, or you can just leave the second argument as is and write if(!tasks.length) {...} or if(task.length === 0) {...} before making the API call, so it only runs once. You can find some more info about this in the extremely helpful react documentation: https://reactjs.org/docs/hooks-effect.html More on reddit.com
🌐 r/reactjs
16
4
December 26, 2020
UseEffect Dependency Array
High-level breakdown of useEffect: no deps array === will run on every render of the component empty deps array === will run only when the component mounts stuff in deps array (i.e [setWorkHoursTimeAndCount]) === runs anytime a value in that deps array is updated The first time you got the error, it was telling you that you were using setWorkHoursTimeAndCount but you did not include it in your deps array. Generally, you get this error as a helper from eslint to make sure you include all the deps so it ensures the value being used inside of useEffect is the most up-to-date version. Then you would use if/else conditions to ensure you're running what's inside only when you really need to and it's explicit. In cases like setters provided by React, it's really not necessary but eslint is not smart enough to know. In these cases, you can find the eslint rule and disable it (not really a good idea if you're a beginner) or add a eslint-disable-next-line react-hooks/exhaustive-deps. I'm surprised you're getting this because I believe eslint has fixed this on their end to ensure this error doesn't show anymore. More on reddit.com
🌐 r/reactjs
13
12
June 8, 2022
Why useEffect dependency array still isn’t automated?
How would you like to see the dependency array being automated? The dependency array gives you the flexibility to make sure that useEffect or useCallback (or any other hook with dependency arrays) is only activated or refreshed once one of the dependencies changes. More on reddit.com
🌐 r/reactjs
12
2
August 26, 2021
People also ask

Can you use objects or arrays in the useEffect dependency array?
Yes, but JavaScript compares object or array references, not their contents. This can cause unnecessary re-renders. To avoid this, use the useMemo hook to memoize objects or arrays used in the dependency array.
🌐
dhiwise.com
dhiwise.com › post › understanding-the-importance-of-the-useeffect-dependency-array-in-react
UseEffect Dependency Array
What happens if you don’t pass a dependency array to useEffect?
Without a dependency array, the effect runs after every render. This can lead to performance issues and bugs since the effect won't re-run based on changes in specific dependencies.
🌐
dhiwise.com
dhiwise.com › post › understanding-the-importance-of-the-useeffect-dependency-array-in-react
UseEffect Dependency Array
What is the useEffect hook used for in React?
The useEffect hook in React allows developers to manage side effects like data fetching, DOM manipulation, or subscriptions within functional components. It helps control when these side effects should be executed by specifying dependencies.
🌐
dhiwise.com
dhiwise.com › post › understanding-the-importance-of-the-useeffect-dependency-array-in-react
UseEffect Dependency Array
Top answer
1 of 7
195

You can pass JSON.stringify(outcomes) as the dependency list:

Read more here

useEffect(() => {
  console.log(outcomes)
}, [JSON.stringify(outcomes)])
2 of 7
23

Using JSON.stringify() or any deep comparison methods may be inefficient, if you know ahead the shape of the object, you can write your own effect hook that triggers the callback based on the result of your custom equality function.

useEffect works by checking if each value in the dependency array is the same instance with the one in the previous render and executes the callback if one of them is not. So we just need to keep the instance of the data we're interested in using useRef and only assign a new one if the custom equality check return false to trigger the effect.

function arrayEqual(a1: any[], a2: any[]) {
  if (a1.length !== a2.length) return false;
  for (let i = 0; i < a1.length; i++) {
    if (a1[i] !== a2[i]) {
      return false;
    }
  }
  return true;
}

type MaybeCleanUpFn = void | (() => void);

function useNumberArrayEffect(cb: () => MaybeCleanUpFn, deps: number[]) {
  const ref = useRef<number[]>(deps);

  if (!arrayEqual(deps, ref.current)) {
    ref.current = deps;
  }

  useEffect(cb, [ref.current]);
}

Usage

function Child({ arr }: { arr: number[] }) {
  useNumberArrayEffect(() => {
    console.log("run effect", JSON.stringify(arr));
  }, arr);

  return <pre>{JSON.stringify(arr)}</pre>;
}

Taking one step further, we can also reuse the hook by creating an effect hook that accepts a custom equality function.

type MaybeCleanUpFn = void | (() => void);
type EqualityFn = (a: DependencyList, b: DependencyList) => boolean;

function useCustomEffect(
  cb: () => MaybeCleanUpFn,
  deps: DependencyList,
  equal?: EqualityFn
) {
  const ref = useRef<DependencyList>(deps);

  if (!equal || !equal(deps, ref.current)) {
    ref.current = deps;
  }

  useEffect(cb, [ref.current]);
}

Usage

useCustomEffect(
  () => {
    console.log("run custom effect", JSON.stringify(arr));
  },
  [arr],
  (a, b) => arrayEqual(a[0], b[0])
);

Live Demo

🌐
Devtrium
devtrium.com › posts › dependency-arrays
What are dependency arrays in React? - Devtrium
August 8, 2021 - Dependency arrays are a concept that is tightly coupled to hooks in React (thus also to function components). Some hooks, like useEffect and useCallback have 2 arguments. The first one is a callback (a function), and the second one is the dependency ...
🌐
DhiWise
dhiwise.com › post › understanding-the-importance-of-the-useeffect-dependency-array-in-react
UseEffect Dependency Array
August 13, 2025 - With Rocket.new , you can build ... features faster. Start Building Now! The useEffect dependency array gives you control over when effects run....
🌐
React
react.dev › reference › react › useEffect
useEffect – React
It should return a cleanup function with cleanup code that disconnects from that system. A list of dependencies including every value from your component used inside of those functions.
Find elsewhere
🌐
Bitstack
blog.bitsrc.io › understanding-dependencies-in-useeffect-7afd4df37c96
Understanding Dependencies in useEffect | Bits and Pieces
March 4, 2025 - Rows indicate when the effect should run, columns show the actual dependencies of the effect, and cells specify the array of dependencies to be used when calling useEffect.
🌐
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.
🌐
Zipy
zipy.ai › blog › react-useeffect-dependency-array
React useEffect Dependency Array Guide
May 23, 2024 - For‎‎ example, we may want to update the state of a component in response to a user action, such as clicking a b‎utton. In such ca‎ses, we can use the‎‎ useEffect hook to trigger the update by specifying a dependency array that contains the state variables that we want to watch for changes.‎
🌐
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 - Since `patients` is changed in useEffect, this creates an infinite re-render. You can tell React to skip unnecessarily rerunning the effect by specifying an array of dependencies as the second argument to the useEffect call.
🌐
Medium
egebilge.medium.com › best-practices-for-useeffect-dependency-arrays-in-react-fcb53ef55495
🎯 Best Practices for useEffect Dependency Arrays in React | by Ege Bilge | Medium
April 24, 2025 - In React, the useEffect hook runs after the component mounts and whenever any value in its dependency array changes:
🌐
Ben Ilegbodu
benmvp.com › blog › object-array-dependencies-react-useEffect-hook
Object & array dependencies in the React useEffect Hook | Ben Ilegbodu
January 2, 2021 - However, we rarely want this ... a second parameter, a "dependencies" array, that will only re-run the effect when the values within the array change across re-renders....
🌐
egghead.io
egghead.io › lessons › react-manage-the-useeffect-dependency-array
Manage the useEffect dependency array | egghead.io
[0:54] React useEffect accepts a second argument as an optimization to combat this problem. That second argument is a dependency array where you pass all the dependencies for your side effect.
Published   March 10, 2020
🌐
React
react.wiki › home › react hooks › useeffect dependency array: deep mechanics explained
useEffect Dependency Array: Deep Mechanics Explained | react.wiki
January 1, 2026 - After each render, React compares the new dependency array with the previous one, element by element, using strict equality. ... function UserProfile({ userId }: { userId: number }) { const [user, setUser] = useState(null); useEffect(() => { fetchUser(userId).then(data => setUser(data)); }, [userId]); // ← Dependency array return <div>{user?.name}</div>; }
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › what-are-the-dependencies-in-useeffect-and-why-are-they-important
What are the dependencies in useEffect and why are they important? - GeeksforGeeks
July 23, 2025 - Clean-up Operations: When you need ... i.e. a function containing the code you want to run as a side effect, and an optional array of dependencies....
🌐
Bam
bam.tech › article › how-to-avoid-bugs-in-useeffect
How to manage the useEffect dependency array like a pro?
March 1, 2022 - Discover how AI makes it possible to break free from legacy systems and unlock IT innovation · Discover how market leaders innovate, accelerate, and reinvent themselves alongside Theodo
🌐
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 - This array defines the list of variables that if changed will trigger the callback function. useEffect(() => { // Callback function }, [dependencyArray])
🌐
DEV Community
dev.to › hey_yogini › useeffect-dependency-array-and-object-comparison-45el
UseEffect dependency array and object comparison! - DEV Community
January 5, 2022 - This useEffect hook takes first parameter as a function to perform side effect and second parameter, a dependencies array. If you do not wish to perform side effects on every render (which is the case almost every time), you need to pass something ...