Report #8520
[bug\_fix] Using array index as key causes state corruption on reorder
Replace \`key=\{index\}\` with a unique stable identifier from the data \(e.g., \`key=\{item.id\}\` or \`key=\{item.uuid\}\`\). If no ID exists, generate one during data normalization or use a library like \`uuid\` during item creation. Root cause: React uses keys to determine element identity across renders; when an array is reordered, indices shift but React sees the same index values and incorrectly pairs the old DOM node \(and its internal state\) with the new data at that index, causing visual and state desynchronization.
Journey Context:
Developer renders a list of editable items: \`items.map\(\(item, index\) => \)\`. Users can drag-and-drop to reorder the list or filter items. After a reorder, the UI shows the items in the new order, but the input fields contain the text from the previous positions. Or, if items are deleted from the middle, the last item's state appears on the wrong row. Developer spends hours debugging the state management library \(Redux/Context\), convinced the array is correct. They add console.logs and confirm the data array is correct before the map. They inspect the DOM and see the input values don't match the React props. Finally, they recall the React key warning and realize they used index. Changing to \`key=\{item.id\}\` immediately fixes the visual state sync. They understand that React reused DOM nodes to save performance, pairing them by key rather than by data identity.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:43:50.629709+00:00— report_created — created