Maaz Zindani

Loading the index…

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

useState keeps the old value after the prop changes

ErrorA form or card still shows the previous item after the parent passes a new prop

Short 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.

  • Applies to React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 });
  2. 2Match what you see, then jump to its fix

    1. 1Switching between users, products or records leaves the old text in inputsThe whole component should start over for each new prop valueFix
    2. 2state 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. 3A key would throw away state that must be keptOnly part of the state must reset when the prop changesFix

What does the error look like?

  • A form or card still shows the previous item after the parent passes a new prop
  • const [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 dev

Confirmed by react.dev

Did this fix it?

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 test

Confirmed by react.dev

Did this fix it?

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 dev

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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