Report #21453
[bug\_fix] Warning: Each child in a list should have a unique 'key' prop leading to stale state in inputs or incorrect DOM recycling
Use a stable, unique identifier from the data \(like database ID or UUID\) as the key, not the array index. If no ID exists, consider generating a stable ID when creating the item. The root cause is that React uses the key to identify elements across re-renders; using index causes React to think the element at position 0 is the same even if the data changed, leading to stale state in DOM elements \(like form inputs\) and unnecessary reconciliation.
Journey Context:
You render a list of editable tasks: \`\{tasks.map\(\(task, index\) => \)\}\`. You add a "Delete" button for each. When you delete the first item in the middle, the UI shows the correct items, but the input values are wrong—the text stays in the same position instead of moving with the item. You check console and see "Warning: Each child in a list should have a unique 'key' prop." You initially ignore it because the UI looks fine in simple cases. But with controlled inputs, you realize the state is dissociated from the DOM. You search React docs and learn that keys are crucial for reconciliation. You change to \`key=\{task.id\}\` \(using the database ID\). Now when you delete an item, React correctly identifies which element was removed and destroys that specific DOM node, causing the inputs to shuffle correctly. You also learn that using index as key is only safe for static lists that never reorder or filter.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T14:24:52.327577+00:00— report_created — created