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

185
Visualizações
Unhandled issue when using the clearInterval func in an `onClick` event in the react

Please understatnd that this is the first post.

I'm developing a dashboard. But there's a problem.

I used to use setInterval, but I wrote a code that performs clearInterval when I press the button.

import React, {useEffect, useRef} from 'react';

const testInterval = () => { 
    const tick = useRef(null);

    useEffect(() => { 
        tick.current = setInterval(() => console.log("mounted."), 1000);

        return () => {
        console.log("unmounted.");
            
        if(!tick.current) {
           //PopupActions.hide();
        } else {
           clearRef(); 
        }
    };
 }, []);

    const clearRef = () => {
        clearInterval(tick.current);
        tick.current = null;
    }

    return (
        <div>
            /* problem
            <button onClick={clearRef}>blahblah</button>
            */
        </div>
    );
}

export default testInterval;

when I quickly return to the original page from the prev page and click the button, the clearInterval func will not work intermittently and will continue to output the log "mounted"

Why is this a problem? And how can we solve this?

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