Picture a shopping list with a note field on each row:
{ items . map ( ( item , i ) => ( < li key = { i } > { item . name } < input placeholder = "note" /> </ li > ) ) }
Enter fullscreen mode
Exit fullscreen mode
The list is Milk, Eggs, Bread. You type "2 dozen" next to Eggs. Then you delete Milk.
The list now shows Eggs and Bread, and "2 dozen" sits next to Bread.
Nothing crashed, nothing warned. The note moved to the wrong row because of key={i} .
The mental model: name tags, not seat numbers
Every render, React has to answer one question for each item in the list: "Is this the same item as last time, or a new one?"
It answers by looking at the key , like checking name tags at a door.
A key is a name tag. React matches items between renders by name tag, not by position.
If the tag matches, React keeps that item's state and DOM element and only updates what changed. If a tag disappears, that item is removed.…