Search code examples
javascriptreactjscallbackreact-functional-component

React interval using old state inside of useEffect


I ran into a situation where I set an interval timer from inside useEffect. I can access component variables and state inside the useEffect, and the interval timer runs as expected. However, the timer callback doesn't have access to the component variables / state. Normally, I would expect this to be an issue with "this". However, I do not believe "this" is the the case here. No puns were intended. I have included a simple example below:

import React, { useEffect, useState } from 'react';

const App = () => {
  const [count, setCount] = useState(0);
  const [intervalSet, setIntervalSet] = useState(false);

  useEffect(() => {
    if (!intervalSet) {
      setInterval(() => {
        console.log(`count=${count}`);
        setCount(count + 1);
      }, 1000);
      setIntervalSet(true);
    }
  }, [count, intervalSet]);

  return <div></div>;
};

export default App;

The console outputs only count=0 each second. I know that there's a way to pass a function to the setCount which updates current state and that works in this trivial example. However, that was not the point I was trying to make. The real code is much more complex than what I showed here. My real code looks at current state objects that are being managed by async thunk actions. Also, I am aware that I didn't include the cleanup function for when the component dismounts. I didn't need that for this simple example.


Solution

  • The first time you run the useEffect the intervalSet variable is set to true and your interval function is created using the current value (0).

    On subsequent runs of the useEffect it does not recreate the interval due to the intervalSet check and continues to run the existing interval where count is the original value (0).

    You are making this more complicated than it needs to be.

    The useState set function can take a function which is passed the current value of the state and returns the new value, i.e. setCount(currentValue => newValue);

    An interval should always be cleared when the component is unmounted otherwise you will get issues when it attempts to set the state and the state no longer exists.

    import React, { useEffect, useState } from 'react';
    
    const App = () => {
        // State to hold count.
        const [count, setCount] = useState(0);
    
        // Use effect to create and clean up the interval 
        // (should only run once with current dependencies)
        useEffect(() => {
            // Create interval get the interval ID so it can be cleared later.
            const intervalId = setInterval(() => {
                // use the function based set state to avoid needing count as a dependency in the useEffect.
                // this stops the need to code logic around stoping and recreating the interval.
                setCount(currentCount => {
                    console.log(`count=${currentCount}`);
                    return currentCount + 1;
                });
            }, 1000);
    
            // Create function to clean up the interval when the component unmounts.
            return () => {
                if (intervalId) {
                    clearInterval(intervalId);
                }
            }
        }, [setCount]);
    
      return <div></div>;
    };
    
    export default App;
    

    You can run the code and see this working below.

    const App = () => {
        // State to hold count.
        const [count, setCount] = React.useState(0);
    
        // Use effect to create and clean up the interval 
        // (should only run once with current dependencies)
        React.useEffect(() => {
            // Create interval get the interval ID so it can be cleared later.
            const intervalId = setInterval(() => {
                // use the function based set state to avoid needing count as a dependency in the useEffect.
                // this stops the need to code logic around stoping and recreating the interval.
                setCount(currentCount => {
                    console.log(`count=${currentCount}`);
                    return currentCount + 1;
                });
            }, 1000);
    
            // Create function to clean up the interval when the component unmounts.
            return () => {
                if (intervalId) {
                    clearInterval(intervalId);
                }
            }
        }, [setCount]);
    
      return <div></div>;
    };
    
    ReactDOM.render(<App />, document.getElementById('app'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
    
    <div id="app"></div>