The neighbourhood head’s row of puppets: Rara claps for Semar and writes a note, then Bagong is added in front or the row turned round, and an index, id or…
Why it matters
A wrong key is one of the most confusing React bugs: the data is right, yet the UI shows it in the wrong place (an input’s text, focus, an animation). Very common because an index "feels good enough", until the list is inserted into, reordered or filtered.
The key idea
A key is an item’s IDENTITY to React, not its place
Common mistakes
A row’s old count sticks to the item just inserted That is key={index}: identity = place. Use a stable id from the data, key={item.id}.
The component seems to "reset" itself every render, input focus jumps The key changes every render, often from key={Math.random()}. A key must stay the same across renders for the same item.
The console says "two children with the same key" The key was made from a value that can repeat (a name, say). Use an id that is unique among siblings.
Try it in this playground
key={i}: add at the front Semar's claps and note move to Bagong: state sticks to the place.
key={item.id}: again Semar keeps what is his; Bagong gets a new Baris.
Turn the row round with key={i} Every row shows another puppet, carrying the old state.
key={Math.random()}: strike the gong The same data, yet every row is unmounted and mounted anew: claps and notes are gone.