AceDevHub
Beginner React Interview QuestionsBeginnerScenario

React · Question 19

When is using an array index as a React key a problem?

Direct answer

An array index is a poor key when items can be inserted, deleted, filtered, or reordered because the same index can become associated with different data.

List behaviorIndex key risk
Static list that never changes orderOften low
Insert or delete itemsComponent identity can shift
Sort or reorder itemsState may follow the position instead of the item
Editable rowsUser input can appear attached to the wrong row

Prefer a stable ID from the data. Generating keys with Math.random() during render is even worse because every render creates new identities.