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():
1 // ⚠️ Anti-pattern: Using array index as key
2 { items . map ( ( item , index ) => (
3 < ListItem key = { index } data = { item } />
4 ) ) }
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.
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):
1 Initial State:
2 - Slot 0 (key=0): Item A [Input: "Task A"]
3 - Slot 1 (key=1): Item B [Input: "Task B"]
4 - Slot 2 (key=2): Item C [Input: "Task C"]
5
6 User prepends Item X to the front of the list:
7 - Slot 0 (key=0): Item X [Input: "Task A" ?? BUG!]
8 - Slot 1 (key=1): Item A [Input: "Task B" ?? BUG!]
9 - Slot 2 (key=2): Item B [Input: "Task C" ?? BUG!]
10 - Slot 3 (key=3): Item C [Input: "" ?? Empty!]
What Went Wrong? 1 React checked key=0. It found that an element with key=0 already existed previously.
2 React kept the existing DOM node and its internal input state ("Task A"), assuming it was the same item!
3 It updated the props of key=0 to Item X, but the user's typed text stayed attached to the wrong item !
4 The newly inserted item X now displays Task A's text, and the last item appears blank or corrupted.
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). Using an index as a key is strictly acceptable only when all three of the following conditions are simultaneously met:
1 The list is completely static (items will never be added, removed, or reordered).
2 The items have no unique IDs in the data source.
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.
1 // If items lack IDs, generate them ONCE upon data creation:
2 const addItem = ( text ) => {
3 setItems ( prev => [
4 { id : crypto . randomUUID ( ) , text } , // Unique stable ID attached to data
5 ... prev
6 ] ) ;
7 } ;
Interactive Quiz • Knowledge Check
Practice Quiz Why is using an array index as a key (key={index}) discouraged in dynamic React lists?
A It causes the web page to consume 10GB of RAM
B Indices reflect array positions rather than item identities, causing state corruption and UI bugs when items are reordered, inserted, or removed
C Arrays in JavaScript cannot have indices greater than 10
D It violates the W3C HTML5 specification
What type of bug commonly occurs when items with input fields use array indices as keys and an item is deleted?
A The browser crashes immediately
B The deleted item's input text or checkbox status lingers and appears on a completely different item
C The entire website reverts to plain HTML
D The database drops the customer's account
When is it safe to use array indices as keys?
A Only when developing on Windows
B When the list is completely static (never filtered, sorted, added, or deleted) and items have no internal state
C In all e-commerce checkout forms
D Whenever you are using Tailwind CSS
If your data items lack a natural ID, what is the best practice?
A Generate a stable unique ID (e.g. using crypto.randomUUID()) when the data is created and store it with the item
B Call Math.random() inside the JSX .map() callback
C Never render the list
D Use the index multiplied by 1,000
Does using key={index} prevent React's console warning about missing keys?
A No, the warning will still appear
B Yes, it silences the warning, which is why it can dangerously hide underlying state synchronization bugs
C Only in Firefox
D Only when running Vite with HTTPS