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

74
Visualizações
react hook useEffect cleanup with dynamic prop problem

In our application we are attempting to leverage useEffect to make an api call on cleanup of a component, something like this:

useEffect(() => {
  return () => {
    myAPIFunction(dynamicProp)
    // api call here that leverages a prop
  }
})

The idea here is that we want to make this api call on the tear down of this component. The problem is that as our application progresses dynamicProp changes at the component level, but whenever the api call is made we get the value dynamicProp as whatever it was initialized at.

If we update it it to the following:

useEffect(() => {
  return () => {
    myAPIFunction(dynamicProp)
    // api call here that leverages a prop
  }
}, [dynamicProp])

It does update the prop in the cleanup function but we really only want to call this once on the component cleanup, and this causes the cleanup to fire any time dynamicProp changes.

Is there a solution where we can have dynamicProp be .... dynamic in our cleanup without having to run the cleanup every time dynamicProp changes?

about 4 years ago · Juan Pablo Isaza
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