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

160
Visualizações
Custom use effect comparator : ReactJS

I am having a state variable that is defined as follows

  const [val, setVal] = useState({ notdone: [], done: [] })

where done has the following structure

[ { col: "val1", field: "val1", values: [1,2,3]}, { col: "val2", filed: "val2", values:[]  }

I want to run the useffect when some changes are made to the done and also values array under each object. In my case, useEffect gets triggered when I remove/add an objects to the done. But not when some changes happen inside values array of each object.

How can I have a watch on both the changes

useEffect(() => {
    callback();
 }, [val.done.length]);

It is achievable using use-deep-compare-effect, but is there a way to do this without using this external package?

Help would be appreciated

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Using the outer array length alone isn't a strong enough comparison. The best counter-example against it is an array mutation where an element is removed while another is added, the result is an array of the same length.

React's Object.is equality check also doesn't do the deep equality check you seek.

The only way I know of to do this without an external dependency is a bit of a hack, but it works, is to JSON stringify* the array so a full string comparison is used.

useEffect(() => {
  callback();
}, [JSON.stringify(val.done)]);

* Source is basically a twitter conversation from Dan Abramov

Deep equality checks are generally a bad idea. If the inputs are shallow enough that you think deep equality would still be fast, consider just using [JSON.stringify(obj)] instead. Note it won’t compare functions.

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