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

Read more here

useEffect(() => {
  console.log(outcomes)
}, [JSON.stringify(outcomes)])
Answer from Loi Nguyen Huynh on Stack Overflow
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

🌐
DhiWise
dhiwise.com › post › understanding-the-importance-of-the-useeffect-dependency-array-in-react
UseEffect Dependency Array
August 13, 2025 - What is the Dependency Array in useEffectEmpty Dependency ArrayHow the Dependency Array Works in the Render CycleWhy You Get Missing Dependency WarningsCommon Dependency Array Mistakes and FixesEmpty Array vs No ArrayThe Role of Cleanup FunctionState Variables and Re RendersAdvanced: Functions ...
Discussions

Can someone explain useEffect dependency arrays like im 5?
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. More on reddit.com
🌐 r/reactjs
24
34
April 24, 2022
reactjs - In useEffect, what's the difference between providing no dependency array and an empty one? - Stack Overflow
It will run before every useEffect code run, to clean up for the previous useEffect run. (Except the very first useEffect run) ... You forgot to mention that the cleanup function will also always run on unmount. So, for example, if the dependency array is empty ([]), then the cleanup function ... More on stackoverflow.com
🌐 stackoverflow.com
reactjs - How to manage dependency arrays with useEffect - Stack Overflow
If I do this REACT will throw a ... missing dependenecy, varA and ask that it be added to the array. But doing so will create behavior I do not want. How can I handle the warning in this situation? ... Ignore the warning. You are correct in your thinking. You do not need the other variable ... One of the rules of React hooks is that useEffect must include ... More on stackoverflow.com
🌐 stackoverflow.com
reactjs - useEffect and dependency array - Stack Overflow
useEffect will re-run with an object in it's dependency array whenever that object changes references. More on stackoverflow.com
🌐 stackoverflow.com
People also ask

Why is the dependency array important in useEffect?
The dependency array in useEffect tells React when to re-run the effect. It ensures the effect only runs when specified state or prop values change, improving performance and avoiding unnecessary renders.
🌐
dhiwise.com
dhiwise.com › post › understanding-the-importance-of-the-useeffect-dependency-array-in-react
UseEffect Dependency Array
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
🌐
W3Schools
w3schools.com › react › react_useeffect.asp
React useEffect Hooks
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.
🌐
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 - If you have an object or array as a dependency of useEffect, and then update that object inside the effect, you effectively create a new object with a new reference and cause an infinite loop.
🌐
Zipy
zipy.ai › blog › react-useeffect-dependency-array
React useEffect Dependency Array Guide
May 23, 2024 - Learn to use useEffect dependency array, single & multiple state variable, empty dependency array, useEffect infinite loop, functions in dependency array,& more
🌐
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])
Find elsewhere
🌐
Medium
medium.com › better-programming › understanding-the-useeffect-dependency-array-2913da504c44
Understanding the useEffect Dependency Array | by Denny Scott | Better Programming
March 10, 2020 - What is a useEffect Hook? What is a dependency array? Do functions work in a dependency array? Can we use mapStateToDispatch with useEffect? Before we jump in, I should answer right away; there is a useDispatch Hook made available by React Redux. 223K followers ·
🌐
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.

🌐
DEV Community
dev.to › rozenmd › understanding-useeffect-the-dependency-array-obg
Understanding useEffect: the dependency array - DEV Community
July 30, 2020 - By default, useEffect always runs after render has run. This means if you don’t include a dependency array, and you’re using useEffect to fetch data to display it, you will always trigger another render after useEffect runs.
🌐
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.
Top answer
1 of 3
5

useEffect will re-run with an object in it's dependency array whenever that object changes references.

For example in your code, user is always stable until you call setUser in which case it will change to a new reference and run at that point.

If you instead defined user as a new object in each render, then the useEffect would instead run every time the component re-rendered.

  const user = {
    firstName: 'Joe',
    lastName: 'Smith',
    address: {
      home: '123 street',
    },
  };

It's all about referential equality. Whether previousUser===newUser on each render. React essentially performs a check very similar to that (I believe Object.is) for every variable in the dependency array.

2 of 3
1

Bellow your component.

changes:

  • move initialState outside UseEffectHook. You don't need to create initialState every re-render;
  • use useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed;
  • setUser and setCount with updater function;
  • handleOnChangeUser able to use for all fields of user object. Every time handleOnChangeUser - create new ref to user object. in this case useEffect able to detect changes;

import React, { useCallback, useEffect, useState } from 'react';

const initialState = {
  firstName: "Joe",
  lastName: "Smith",
  address: {
    home: "123 street",
  }
};

export function UseEffectHook() {
  const [user, setUser] = useState(initialState);
  const [count, setCount] = useState(0);
  const handleOnChangeUser = useCallback((event) => {
    const { name, value } = event;
    
    setUser(prevState => ({ ...prevState, [name]: value }));
  }, []);
  
  const increaseCount = useCallback(() => {
    setCount(prevState => prevState + 1);
  }, []);

  useEffect(() => {
    console.log("triggered");
  }, [user]);

  return (
    <div>
      <p>useEffect - Practice with different deps</p>
      {JSON.stringify(user)}

      <label>
        <span>Firstname:</span>

        <input
          type="text"
          name="firstName"
          onChange={handleOnChangeUser}
        />
      </label>
      
      <br />
      
      <label>
        <span>Lastname:</span>
        
        <input
          type="text"
          name="lastName"
          onChange={handleOnChangeUser}
        />
      </label>

      <button onClick={increaseCount}>Increase</button>
   </div>
  );
}

🌐
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 ...
🌐
Devtrium
devtrium.com › posts › dependency-arrays
What are dependency arrays in React? - Devtrium
August 8, 2021 - It's a pretty simple rule, made a bit harder by some exceptions. The rule is: if any variable is used inside the hook but is defined outside of it, then it should go in the dependency array.
🌐
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 - When working with React’s useEffect hook, understanding how to use dependency arrays correctly is essential for writing performant and…
🌐
Medium
medium.com › @vishalthakur2463 › what-kind-of-values-does-the-useeffect-dependency-array-accept-c443738107c7
What Kind of Values Does the useEffect Dependency Array Accept? | by Vishal Solanki | Medium
September 20, 2025 - However, understanding what kinds of values go into this array and how React reacts to them can be tricky. So, let's break it down step by step, with real-world examples. The dependency array in useEffect can accept any type of JavaScript value.
🌐
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....