- Home
- /
- Tutorials
- /
- React Tutorial
- /
- Keys Explained
Lists and Keys
Keys Explained
A key is an identity, not a position. Using the array index says "the third item is whatever is currently third", which is exactly wrong the moment the list reorders.
What React uses them for
When a list re-renders, React matches old elements to new ones by key. A matched element is updated in place and keeps its state; an unmatched one is destroyed and rebuilt.
That matching is the entire purpose. Without keys React falls back to position, and warns you in the console because position is a poor guess.
Where index keys go wrong
This is easier to see than to describe. Both lists below hold the same data and differ only in their key. Type into the first input of each, then press Remove.
The classic index-key bug
jsx
