Report #73400
[bug\_fix] List state corruption when using array index as React key
Use a unique and stable identifier from your data \(such as a database ID, UUID, or slug\) as the \`key\` prop instead of the array \`index\`. Root cause: React uses keys to identify elements across re-renders; when you use indices, React thinks the element at position 0 is the same even when the underlying data changes, causing it to preserve the wrong internal state \(like form inputs or expanded states\) on the wrong DOM nodes.
Journey Context:
You have a todo list with checkboxes. You add three items: "Buy milk" \(unchecked\), "Walk dog" \(checked\), "Call mom" \(unchecked\). You delete the first item \("Buy milk"\). Suddenly, "Walk dog" appears unchecked and "Call mom" appears checked—the checkbox states swapped. You inspect the React DevTools and see the component props are correct, but the DOM shows wrong checked states. You check your code and see you're doing \`todos.map\(\(todo, index\) => \)\`. You realize that when you delete index 0, the item that was at index 1 \("Walk dog"\) becomes index 0. Because the key is 0 in both cases, React thinks it's the same element and preserves the DOM node \(including the checked state\) but updates the text content. You change it to \`key=\{todo.id\}\` \(the stable database ID\), and now when you delete an item, React correctly removes that specific DOM element because the unique key disappeared, preventing state corruption.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T05:47:39.815486+00:00— report_created — created