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

92
Views
clearInterval created in useEffect hook from outside React component

I have this code, i'd like to clearInterval from outside this component (let's assume that I render a button with onClick method that calls clearInterval function). I tried to pass interval value as a ref, but as soon as state updates this value changes.

  useEffect(() => {
    (intervalRef.current as ReturnType<typeof setInterval> | undefined) = setInterval(() => {
      fetch().then((items) => {
        items.forEach((item, id) => {
         // set state based on the values
        });
      });
    }, 5000);

    return () => clearInterval(intervalRef.current);
  }, [state]);

I'm also getting eslint warning for return () => clearInterval(intervalRef.current), so I assume that it won't clear properly inside this method as well.

"The ref value 'intervalRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'intervalRef.current' to a variable inside the effect, and use that variable in the cleanup function."

What is the correct approach of such issues in React?

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

0

useEffect(() => {
    const intervalId = setInterval(() => {
      fetch().then((items) => {
        items.forEach((item, id) => {
         // set state based on the values
        });
      });
    }, 5000);

    return () => clearInterval(intervalId);
}, [state]);

You dont need to make it a ref, if all you want is to clean the interval on unmount. Just assign it to a variable and pass it to your cleanup function.

Note: i see you are planning on doing a state update in promise. you might run into issue where component is unmounted and then the promise callback is triggered which tries to update state. so maybe put a isMounted check

about 4 years ago · Juan Pablo Isaza Report

0

"The ref value 'intervalRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'intervalRef.current' to a variable inside the effect, and use that variable in the cleanup function."

This is saying to save a reference to the ref value in the useEffect hook's callback closure, to be used in the cleanup function.

useEffect(() => {
  (intervalRef.current as ReturnType<typeof setInterval> | undefined) = setInterval(() => {
    fetch().then((items) => {
      items.forEach((item, id) => {
       // set state based on the values
      });
    });
  }, 5000);

  const timerId = intervalRef.current;

  return () => clearInterval(timerId);
}, [state]);

If you need to, or want to, clear the interval from anywhere else in the component scope, just clear the interval as you would normally with the current ref value:

clearInterval(intervalRef.current);
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!