Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda