useState keeps the old value after the prop changes
A form or card still shows the previous item after the parent passes a new propShort answer
useState uses its argument only on the first render. When the prop changes later, the state keeps the old value. Reset the component by giving it a key tied to the prop, or stop copying the prop into state and compute the value during render. Do not sync it with useEffect.
Fix it fast
1Find out which cause you have
Find the useState call whose argument is a prop, and add a log next to it. If the prop changes in the log but the state does not, this page applies.
const [comment, setComment] = useState(initialComment); console.log({ initialComment, comment });2Match what you see, then jump to its fix
- 1
Switching between users, products or records leaves the old text in inputsThe whole component should start over for each new prop valueFix - 2
state and prop hold the same value, and an effect or handler tries to keep them equalThe state is a copy of a value that can be calculatedFix - 3
A key would throw away state that must be keptOnly part of the state must reset when the prop changesFix
- 1
What does the error look like?
A form or card still shows the previous item after the parent passes a new propconst [value, setValue] = useState(props.value) never changes
Cause 1 of 3
The whole component should start over for each new prop value
1Check
The initial value is ignored after the first render, so the component keeps its state while the parent only changes props. A different key makes React treat it as a new component.
grep -rn "useState(props\.\|useState(initial" src/2Fix
In the parent, pass the identifying prop as key. React then re-creates the component and all of its children, so every useState starts from the new prop.
export default function ProfilePage({ userId }) {
return <Profile userId={userId} key={userId} />;
}
function Profile({ userId }) {
const [comment, setComment] = useState('');
// resets whenever userId changes
}3Verify
Switch records in the UI; the inputs start empty or with the new record's values.
npm run devConfirmed by react.dev
Cause 2 of 3
The state is a copy of a value that can be calculated
1Check
If the state can be worked out from props or other state, it should not be state. Selecting by id is the common case: storing the whole selected object goes stale when the list changes.
grep -rn "setSelected\|setSelection" src/2Fix
Store only the id and derive the object while rendering.
const [selectedId, setSelectedId] = useState(null);
const selection = items.find(item => item.id === selectedId) ?? null;3Verify
Replace the items list; the selection follows it without any extra code.
npm testConfirmed by react.dev
Cause 3 of 3
Only part of the state must reset when the prop changes
1Check
If just one piece should reset, a key is too blunt. Keep the previous prop in state and compare it while rendering. This is the last-resort pattern in the React docs.
grep -rn "useEffect" -A 3 src/ | grep "set"2Fix
Compare with the stored previous prop during render, update both pieces of state, and make sure the condition becomes false after the update or it loops. Only the same component's state may be set this way.
const [prevItems, setPrevItems] = useState(items);
if (items !== prevItems) {
setPrevItems(items);
setSelection(null);
}3Verify
Change the prop once and check the component renders twice at most, with no loop warning.
npm run devConfirmed by react.dev
How do I stop it coming back?
- Name a prop that only seeds state initialX, so readers know later changes are ignored.
- Prefer deriving values in render over storing copies.
- Avoid a useEffect that only calls set: it renders once with the stale value first.
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
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 fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix