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

163
Visualizações
How React know it has to run cleanup function on unmount/dependency update?

This question is something more related to understanding internal workings of useEffect. I have read few of the articles but couldn’t find the answer to the question.

What's useEffect execution order and its internal clean-up logic in react hooks?

https://www.bussieck.com/useeffect-under-the-hood/

My question specifically is, “How react know it has to run cleanup function?” When I see useEffect, it takes a callback function and it return a function which is optional and which doesn’t get invoked instantly. So how react figures out to call it on unmount/dependency update?

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

0

So you've already read that ordering really matters and React stores "internal data" per hook's position. Similarly how useState stores "current value", useEffect also can save some internal data: last values for dependencies list(initially it's []) and value returned when last time this specific useEffect has run(initially undefined).

Then once internal comparison for dependencies says "ok, dependencies are different, need to run this useEffect again" React takes result from last call and whether it's a function - runs it without any arguments(cleanup).

Next step effect itself is executed - and whathever it returns - is stored back into slot for current hook, to be checked/run next time.

Sure, also cleanup will be run on unmount. If it is a function.

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