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

145
Views
Can't perform a React state update on an unmounted component for setInterval

I get a warning " Can't perform a React state update on an unmounted component " when i am running this code

function timer() {

    const [dateState, setDateState] = useState(new Date());
    useEffect(() => {
            setInterval(() => setDateState(new Date()), 1000);
    }, []);

    return(
        <div className="container">
        <p className="text-end text-danger">{dateState.toLocaleDateString('fr-BE' , {
                day : 'numeric',
                month : 'short',
                year: 'numeric'
            })}</p>
        <div className="text-center">
            <h3 className="text-info">
                {dateState.toLocaleString('fr-BE', {
                    hour: 'numeric',
                    minute: 'numeric',
                    second : 'numeric',
                    hour24: true,
                })}
            </h3>
            </div>
        </div>
    )
}

export default timer;

I tried to do

    const [dateState, setDateState] = useState(new Date());
    useEffect(() => {
        let mount = true;
        if(mount){
            setInterval(() => setDateState(new Date()), 1000);
        }
        return () => { isMounted = false };
    }, []);

But still not working, i'm kind of stuck...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to cleanup the setInterval timer on component unmount, like:

useEffect(() => {
  const interval = setInterval(() => setDateState(new Date()), 1000);

  return () => {
    clearInterval(interval);
  }
}, []);
about 4 years ago · Juan Pablo Isaza Report
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!