Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
if a useState holds an object, does setting it to an identical object trigger downstream dependencies?
const [stuff, set_stuff] = useState{a: 1}


...

set_stuff({a: 1})

when set_stuff({a: 1}) is run, will it trigger downstream dependencies that listen to stuff?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If they listen to stuff, yes.

If they listen to a property of stuff that's equal to the previous value at that property (like a and 1), no. Example:

const App = () => {
  const [stuff, setStuff] = React.useState({a: 1});
  React.useEffect(() => {
    console.log('Will trigger later because stuff changed');
  }, [stuff]);
  React.useEffect(() => {
    console.log('Will not trigger later because stuff.a did not change');
  }, [stuff.a]);
  
  React.useEffect(() => {
    setTimeout(() => {
      setStuff({ a: 1 });
    }, 1000);
  }, []);
  return 'foo';
};

ReactDOM.render(<App />, document.querySelector('.react'));
<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 class='react'></div>

about 4 years ago · Juan Pablo Isaza Report

0

React does not do the deep comparison

If you want to trigger on value change do like below

  useEffect(() => {
    console.log('Something');
  }, [stuff.a]);
about 4 years ago · Juan Pablo Isaza Report

0

If you listen the object stuff, it will trigger downstream dependencies because the two identical object is not really identical in computer. When you set_stuff, you just changed the value of stuff by a new address pointer.

const stuffOne = {a: 1}
const stuffTwo = {a: 1}
console.log(stuffOne === stuffTwo) // false

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!