Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

48
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?

5 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos