Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!