Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda