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

176
Visualizações
useEffect doesn't work correctly even when setting the dependency

I have a useEffect function filters out two arrays but for some reason it isn't working correctly. If I remove the dependency array then the brings back the correct data but it keeps on looping. enter image description here

I've then added the dependency array back in, but then it comes back empty. Any ideas as to where I'm going wrong?

useEffect(() => {
  if (extras.length && pkg ? .id) {
    setVisibleExtras(
      extras.filter(({
          availableOnTariff
        }) =>
        availableOnTariff.some(({
          id
        }) => id === pkg.id)
      )
    );
    console.log('visible extras', visibleExtras);
  }
}, [extras, pkg]);
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

If setVisibleExtras changing extras it will always trigger whole useEffect again... if its your case, use callback function in setVisibleExtras instead:

useEffect(() => {
  if (extras.length && pkg ? .id) {
    setVisibleExtras((extras) =>
      extras.filter(({
          availableOnTariff
        }) =>
        availableOnTariff.some(({
          id
        }) => id === pkg.id)
      )
    );
    console.log('visible extras', visibleExtras);
  }
}, [pkg]);
about 4 years ago · Santiago Gelvez 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