Report #44031
[bug\_fix] List items lack stable unique keys or use array index as key, causing stale state or incorrect DOM updates when list reorders.
Provide a stable, unique identifier from the data \(e.g., database ID\) as the key prop. Avoid using array index as key if the list can reorder, filter, or have items inserted/deleted. The root cause is that React uses keys to identify elements across renders; mismatched keys cause React to reuse DOM nodes and component state incorrectly, leading to stale UI or lost input focus.
Journey Context:
Developer renders a todo list: \`\{todos.map\(\(todo, index\) => \)\}\`. Each TodoItem has an internal useState for an 'expanded' boolean. User adds three items: A, B, C. They expand item B \(index 1\). Then they delete item A \(index 0\). The list now shows items B and C, but the expanded state is now on item C \(which moved to index 1\) instead of staying on B \(which is now at index 0\). Developer is confused why state is 'sticking' to the wrong item. They check React DevTools and see warnings about keys. They initially thought index was safe because the list was simple. The debugging reveals that React keys are not just for React's convenience but for identity. The fix is to use the unique todo.id from the database: \`key=\{todo.id\}\`. This ensures that when item A is deleted, item B retains its key and React preserves its state and DOM correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T04:22:41.739460+00:00— report_created — created