The Filtering Problem with Index Keys
What you'll learn: Why using array indices as keys breaks React when you filter a list, causing it to update the wrong components and lose state unexpectedly.
The Core Issue
When you filter a list that uses index keys, React sees the "same" keys (0, 1, 2...) before and after the filter, but they now point to different data. React thinks "key 0 still exists, so I'll reuse that component" — but the item at index 0 has completely changed.
This causes three serious problems:
1. Component Identity Mismatch React reuses the component instance that was rendering "Item A" to now render "Item B", because both happened to be at index 0 at different times.
2. Incorrect Animations If you're animating items out, React might animate the wrong elements because it's matching keys, not actual data. The item that should disappear stays, and the wrong item animates away.
3. Corrupted Local State The most dangerous issue: if your list items have internal state (text inputs, checkboxes, expanded/collapsed state), that state stays attached to the key. Filter out the first item? The second item now appears at index 0 with the first item's old state still attached.
The Analogy
Imagine lockers numbered 0, 1, 2. Students store their belongings by locker number. Now filter out student A from locker 0. Student B moves to locker 0, but finds student A's stuff still inside — because the locker number (key) didn't change, even though the student (data) did.
Key Takeaway: Filtering with index keys causes React to misidentify which components correspond to which data, leading to visual bugs, broken animations, and state attached to the wrong items. Always use stable IDs when your list can change.