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

429
Visualizações
useEffect cleanup function

I use the Usage Effect to implement cancellation. Accordingly, there is a delay, which is carried out in debounce. The use effect, as I understand it, makes the information leak and an empty function. How to make it so that if there is no "cost" State, then the function will not work and there will be no warning

Mistake:Warning: It is not possible to update the reaction state for the unmounted component. This is not an operation, but it indicates a memory leak in your application. To fix this, cancel all subscriptions and asynchronous tasks in the usage effect cleanup function.

Code:

    function useDebounce(value,fn, delay) {
      useEffect(() => {
          const handler = setTimeout(() => { fn() }, delay);
          return () => {  clearTimeout(handler) };
        },[value]);
    }

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

0

You can create a mounted check.

function useDebounce(value,fn, delay) {
  useEffect(() => {
    let mounted = true;
    const handler = setTimeout(() => {
       if (mounted) fn();
    }, delay);
    return () => {
      mounted = false;
      clearTimeout(handler);
    };
  },[value]);
}
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