A React timer, interval or handler keeps using an old state value
A setTimeout alert shows the value from before the clickShort 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.
Fix it fast
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'2Match what you see, then jump to its fix
- 1
A 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
setInterval in useEffect(..., []) ticks, but the value never changesAn Effect with a missing dependency keeps the first render's functionFix - 3
Adding the value to the dependencies recreates the timer or connection on every changeThe Effect must read the latest value but should not restartFix
- 1
What does the error look like?
A setTimeout alert shows the value from before the clickA setInterval counter sticks at 1 and never goes higherCalling 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 value2Fix
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 33Verify
Click once and the counter grows by three.
# click the button in the browserConfirmed by react.dev
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 srcConfirmed by react.dev
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/ | head2Fix
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 UIConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
The exhaustive-deps warning on useEffect, and how to clear it properly
React Hook useEffect has a missing dependency: 'fetchData'
The linter found a prop, state value or function the effect reads that is not in its dependency array.
4 causes, each with a fixWeb apps
React state still shows the old value right after setState
three setCount(count + 1) calls raise the counter by 1, not 3
A setter does not change the variable in the code that is running.
2 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fix