Nov 21, 2026
Your list renders the wrong row after a delete — it's the key
The bug report is one sentence: "I deleted the second row and the third row's notes disappeared." The notes are still in the database. The row is still in the list. The state just moved to a different row, and the first place you look — the delete handler, the API, the database — is all fine. The bug is one prop: key={index}.
A key is not an optimisation hint. It is how React answers one question on every render: is this the same item I rendered last time, or a different one? React matches old and new list items by their key. Same key, same component instance — it keeps the useState, the uncontrolled input contents, the focus, the scroll position, the in-flight animation. Different key, it tears the old instance down and mounts a fresh one.
The index makes a bad key because it names a seat, not a person. Walk through a delete with three rows:
{items.map((item, i) => (
<Row key={i} item={item} />
))}
Before the delete: key 0 is Milk, key 1 is Eggs, key 2 is Bread. Delete Eggs. Now the list is [Milk, Bread] — and key 1 is Bread. React's view of it: key 0 is still there, key 1 is still there with new props, key 2 is gone. So it keeps the first two row instances, updates their props, and unmounts the last one. The row that held Bread's unsaved notes is gone; the row that used to be Eggs now shows Bread, keeping whatever local state Eggs' row had.
The classic version is a checkbox. Each row holds its own state:
function Row({ item }: { item: Item }) {
const [selected, setSelected] = useState(false);
// ...
}
Check the first row, delete it, and the checkmark stays — on the row that moved into its place. Nobody selected that row. The state followed the position instead of the item.
It survives code review because it works right up until it doesn't. A list that only ever grows from the end never shows the bug: nothing changes position, so the seat numbers happen to name the same items every time. The bug arrives the week someone adds sorting, filtering, deleting from the middle, drag-and-drop, or live updates — and by then the key={index} is three months old and nobody remembers writing it.
The fix is one line, and it is a rule, not a preference: the key should be a stable, unique identifier of the data, not of its position.
{items.map((item) => (
<Row key={item.id} item={item} />
))}
Delete Eggs now and React sees key milk still there, key eggs gone, key bread still there. It removes exactly the deleted row and leaves the other two instances — and their state — alone.
Two keys to avoid. The first is the index, for the reason above. The second is worse: key={Math.random()}. A new key on every render means React sees a brand-new list every render, tears every row down, and mounts fresh ones. Inputs lose what you typed and lose focus while you type. If your data has no id, add one where the record is created — crypto.randomUUID() when the item enters the store, or the id the API already returns — never during render, where a new value every render is the same bug in a different coat.
There is one place the index is fine: the list never changes order, nothing is inserted or removed in the middle, and the items hold no state. A static list of footer links, a fixed set of nav items — the index is harmless there, and leaving key out entirely just makes React use the index for you and log a warning.
And once you see the key as identity, it becomes a tool. Change a component's key on purpose and React throws the old instance away and mounts a fresh one — all its half-typed state gone. A form keyed by the selected record's id resets itself the moment you pick a different record, no manual clearing, no stale draft from the previous item:
<EditForm key={selectedShop.id} shop={selectedShop} />
The rule I use now: if the list can change — delete, insert, sort, filter, live updates — every item needs a key that names the item, not the seat it happens to sit in. When the state ends up on the wrong row, the database is almost never the problem. React did exactly what the keys told it to. The keys were just lying about who was who.