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

130
Views
React setInterval function keeps running in the background after rerender

I am using setInterval inside useEffect with an empty dependency array inside one of my react components. There is true/false useState which controls the display of that component. When the state is false, the component is hidden and when it's true the component is shown.

Something like this:

const [state, setState] = useState(false)

// in jsx render section

return (
   <div>
      {state? <component/> : '' }
   </div>
)

When the component loads for the first time the setInterval runs only one time which is what it supposes to do.

If the state goes to false then back to true, the component is removed from the UI and is then displayed back again. However, what happens here is now I have two setInterval functions running in the background, and the first one doesn't shutdown.

The number of the setInterval functions keeps increasing with each time that component re-render.

I don't understand how React works in this situation.

I need to know how it works (i.e. why won't the function shutdown, why are the number of functions increasing) and how to fix it.

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

0

This is the structure of React useEffect.React performs the cleanup when the component unmounts.

useEffect(() => {
    //effect
    return () => {
        //cleanup runs on unmount
    }
}, [])

The cleanup function should have clearInterval() which will basically removes setInterval or stops it when component unmounts. See the practical code below:

let intervalid;
useEffect(
    () => {
     intervalid = setInterval(() => {console.log("Iterate");}, 1000));

      return function cleanup() {
        console.log("cleaning up");
        clearInterval(intervalid);
      };
    },
    []
  );

This above code is just for understanding approach. Every effect may return a function that cleans up after it. This lets us keep the logic for adding and removing subscriptions close to each other. # FROM REACT DOCS Reference

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!