Maaz Zindani

Loading the index…

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

A child component keeps showing old data after its prop changed

ErrorThe parent state changes in the console but the child still shows the first value

Short answer

A child shows stale data when something in it froze the prop's value. useState(prop) reads the prop once, and the parent can mutate an array or object so React sees the same reference and skips the render. Use the prop directly, reset the child with a key, and update parent state with new copies.

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

Fix it fast

  1. 1Find out which cause you have

    Log the prop in the child's render body. If it prints the new value while the screen shows the old one, the child copied it into state. If it prints the old value, the parent never produced a new one.

    function Child({ item }) {
      console.log("Child render, prop:", item);
      // ...
    }
  2. 2Match what you see, then jump to its fix

    1. 1The prop in the console is new, the screen is old, and the child has useState(props.something)The child copied the prop into useStateFix
    2. 2The parent used push, sort, or assignment on an array or object in state, and the child shows the old listThe parent mutates state, so the prop keeps the same referenceFix
    3. 3The child has useEffect(() => setValue(prop), [prop]) and the screen lags one render behindAn Effect copies the prop into stateFix

What does the error look like?

  • The parent state changes in the console but the child still shows the first value
  • The child updates only after a full page reload

Cause 1 of 3

The child copied the prop into useState

1Check

The argument to useState is the initial value only. React ignores it on every later render, so a later prop change never reaches that state.

grep -rn "useState(props\.\|useState(item\|useState(user" src

2Fix

If the child does not edit the value, delete the state and read the prop. If it does edit it, reset the child by giving it a key that changes with the data.

// read the prop directly
function Child({ item }) {
  return <h2>{item.name}</h2>;
}

// or reset an editable child when the id changes
<Profile userId={userId} key={userId} />

3Verify

Switch to a different item. The child shows its data straight away.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

The parent mutates state, so the prop keeps the same reference

1Check

React treats state as read-only. Calling push on an array in state changes it in place; the setter then receives the same array and the update is not seen as a change.

grep -rn "\.push(\|\.splice(\|\.sort(" src --include=*.jsx --include=*.tsx

2Fix

Create a new array or object each time. Use spread to add, map to change one item and filter to remove.

setArtists([...artists, { id: nextId++, name }]);

setArtists(artists.map(a => a.id === id ? { ...a, name: "New name" } : a));

setArtists(artists.filter(a => a.id !== id));

3Verify

Click the control that changes the list; the child re-renders with the new list.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

An Effect copies the prop into state

1Check

Copying a prop into state inside an Effect is one of the cases the React docs say does not need an Effect. The state only catches up after an extra render.

grep -rn -B1 -A3 "useEffect" src/components/Child.jsx

2Fix

Compute the value while rendering, or reset all state with a key when the prop is an identity such as an id.

function Child({ items, selectedId }) {
  const selection = items.find(i => i.id === selectedId);
  return <p>{selection?.name}</p>;
}

3Verify

Change the prop and watch the screen: it updates in the same render.

npm run dev

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Treat props as the source of truth. Add state in the child only for data the child itself edits.
  • Never call push, splice, sort or reverse on an array that lives in state; copy it 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.