Avoiding Array Indices as Keys0%

Avoiding Array Indices as Keys

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Avoiding Array Indices as Keys

1. The Temptation of the Array Index

When developers first encounter the React warning about missing keys, the easiest quick fix often seems to be using the array index provided by .map():

JSX
// ⚠️ Anti-pattern: Using array index as key
{items.map((item, index) => (
<ListItem key={index} data={item} />
))}

While this silences the browser console warning, using array indices as keys is a dangerous anti-pattern that can lead to subtle, baffling bugs in stateful or dynamic lists.

2. Why Index as Key Causes Critical Bugs

An array index does not represent the identity of the data item—it only represents its current physical slot in the array.

Scenario: Inserting at the Beginning of a List

Consider a list of 3 items with internal state (such as input fields where the user entered text):

Output
Initial State:
- Slot 0 (key=0): Item A [Input: "Task A"]
- Slot 1 (key=1): Item B [Input: "Task B"]
- Slot 2 (key=2): Item C [Input: "Task C"]
 
User prepends Item X to the front of the list:
- Slot 0 (key=0): Item X [Input: "Task A" ?? BUG!]
- Slot 1 (key=1): Item A [Input: "Task B" ?? BUG!]
- Slot 2 (key=2): Item B [Input: "Task C" ?? BUG!]
- Slot 3 (key=3): Item C [Input: "" ?? Empty!]

What Went Wrong?

  1. 1
    React checked key=0. It found that an element with key=0 already existed previously.
  2. 2
    React kept the existing DOM node and its internal input state ("Task A"), assuming it was the same item!
  3. 3
    It updated the props of key=0 to Item X, but the user's typed text stayed attached to the wrong item!
  4. 4
    The newly inserted item X now displays Task A's text, and the last item appears blank or corrupted.

3. Visual Reproduction: Form Checkboxes and Inputs

This issue manifests most severely in:

  • Lists of items with checkboxes (checking one item and deleting it causes the next item to appear checked).
  • Lists of text inputs where user input is held in local state.
  • Lists with animations or transitions (CSS transitions animate the wrong elements).

4. When Is Using an Index Acceptable?

Using an index as a key is strictly acceptable only when all three of the following conditions are simultaneously met:

  1. 1
    The list is completely static (items will never be added, removed, or reordered).
  2. 2
    The items have no unique IDs in the data source.
  3. 3
    The list items are purely presentational (they contain no internal state, no uncontrolled inputs, and no form controls).

If the list can ever be filtered, sorted, or mutated, you must generate or supply a persistent unique ID.

JSX
// If items lack IDs, generate them ONCE upon data creation:
const addItem = (text) => {
setItems(prev => [
{ id: crypto.randomUUID(), text }, // Unique stable ID attached to data
...prev
]);
};

Interactive Quiz

Practice Quiz

Question 1
Why is using an array index as a key (key={index}) discouraged in dynamic React lists?
Question 2
What type of bug commonly occurs when items with input fields use array indices as keys and an item is deleted?
Question 3
When is it safe to use array indices as keys?
Question 4
If your data items lack a natural ID, what is the best practice?
Question 5
Does using key={index} prevent React's console warning about missing keys?
Next Lesson

Filtering and Sorting List Data Dynamically

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext