Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A React timer, interval or handler keeps using an old state value

ErrorA setTimeout alert shows the value from before the click

Short answer

In React, each render has its own copy of state, and any function created in that render keeps that copy. A timer or Effect that was set up earlier therefore reads old values. Use an updater function when you only need the previous value, list every value the Effect reads in its dependencies, or use useEffectEvent.

  • Applies to React 19, eslint-plugin-react-hooks
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the linter. A missing dependency in an Effect is reported by the react-hooks rule, and it is the most common source.

    npx eslint . --rule 'react-hooks/exhaustive-deps: warn'
  2. 2Match what you see, then jump to its fix

    1. 1A timeout or handler logs the value from when it was created, not the value on screenThe callback reads the state variable of an old renderFix
    2. 2setInterval in useEffect(..., []) ticks, but the value never changesAn Effect with a missing dependency keeps the first render's functionFix
    3. 3Adding the value to the dependencies recreates the timer or connection on every changeThe Effect must read the latest value but should not restartFix

What does the error look like?

  • A setTimeout alert shows the value from before the click
  • A setInterval counter sticks at 1 and never goes higher
  • Calling setCount three times in one handler adds only 1

Cause 1 of 3

The callback reads the state variable of an old render

1Check

Setting state only changes it for the next render. Code that is already running keeps the snapshot it was created with, even when it is asynchronous.

setCount(count + 5);
setTimeout(() => console.log(count), 3000); // logs the old value

2Fix

When the new state depends on the previous one, pass an updater function. React queues it and hands it the latest queued value.

setCount(n => n + 1);
setCount(n => n + 1);
setCount(n => n + 1); // adds 3

3Verify

Click once and the counter grows by three.

# click the button in the browser

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

An Effect with a missing dependency keeps the first render's function

1Check

Leaving a reactive value out of the dependency list, or silencing the linter, makes React keep using the function from the first render.

grep -rn "exhaustive-deps" src/

2Fix

Remove the eslint-disable line. If the Effect only needs the previous state, use an updater function so the dependency is gone.

useEffect(() => {
  const id = setInterval(() => {
    setCount(c => c + 1);
  }, 1000);
  return () => clearInterval(id);
}, []);

3Verify

The counter now increases every second with no lint warning.

npx eslint src

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

The Effect must read the latest value but should not restart

1Check

Some reads, such as an increment step, should always be current without re-running the Effect.

grep -rn "useEffect(" src/ | head

2Fix

Move that logic into an Effect Event. It always sees the latest values and is not a dependency.

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

const onTick = useEffectEvent(() => {
  setCount(c => c + increment);
});

useEffect(() => {
  const id = setInterval(() => onTick(), 1000);
  return () => clearInterval(id);
}, []);

3Verify

Change the increment while the timer runs; the next tick uses the new value without a restart.

# change the value in the UI

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Treat the react-hooks dependency warning as an error, not as noise.
  • Use the updater form whenever the next state is computed from the previous state.
  • Remember that state is a snapshot: reading it right after the setter still gives the old value.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.