Choose stable sibling keys
Choose keys from the data
A key is an identity label React uses when comparing children of the same parent. A reliable key stays attached to its record while that record remains the same. Use IDs already assigned by the data source, not a value derived from its current array position. Distinct sibling records need distinct keys, even when their display names match.
function ExhibitRow({ exhibitId, name }) { return <li>{name} ({exhibitId})</li>;}
export default function App() { const exhibits = [ { id: 'a1', name: 'Compass' }, { id: 'a2', name: 'Compass' } ]; return <main><h1>Collection</h1><ul> {exhibits.map(exhibit => ( <ExhibitRow key={exhibit.id} exhibitId={exhibit.id} name={exhibit.name} /> ))} </ul></main>;}Expected: Compass (a1) and Compass (a2). The key goes on ExhibitRow because that is the element returned by map. A key placed only on the inner li would not identify the siblings in the outer array. React reserves key for matching; ExhibitRow reads exhibitId, an ordinary prop. A key does not create an HTML id. Separate lists can reuse keys because they have different parents.
Group several nodes under one key
import { Fragment } from 'react';
export default function App() { const exhibits = [ { id: 's1', name: 'Moon map', room: 'Room 2' }, { id: 's2', name: 'River model', room: 'Room 4' } ]; return <main><h1>Exhibit locations</h1><dl> {exhibits.map(exhibit => ( <Fragment key={exhibit.id}> <dt>{exhibit.name}</dt> <dd>{exhibit.room}</dd> </Fragment> ))} </dl></main>;}Expected: two terms, each followed by its room description. The Fragment groups the pair for matching without inserting a wrapper DOM element. The shorthand fragment cannot receive a key; import the explicit Fragment form when you need one.
Detect duplicate identifiers before rendering
function hasUniqueIds(exhibits) { const ids = exhibits.map(exhibit => exhibit.id); return ids.every(id => typeof id === 'string' && id.length > 0) && new Set(ids).size === ids.length;}
function ExhibitList({ exhibits }) { if (!hasUniqueIds(exhibits)) return <p>Exhibit identifiers are invalid.</p>; return <ul>{exhibits.map(exhibit => <li key={exhibit.id}>{exhibit.name}</li>)}</ul>;}
export default function App() { return <main><h1>Exhibit records</h1> <ExhibitList exhibits={[{ id: 'x1', name: 'Map' }, { id: 'x1', name: 'Model' }]} /> <ExhibitList exhibits={[{ id: 'x1', name: 'Map' }, { id: 'x2', name: 'Model' }]} /> </main>;}Expected: an invalid-identifiers message for the first dataset and a two-item list for the second. Set is a JavaScript collection that keeps unique values; its size reveals repeats. This simple guard assumes an array of objects with nonempty string IDs; it is not a full external-data validator. React does not repair ambiguous duplicate keys. An array index can be reasonable for a truly fixed list that will never reorder, insert, remove or filter records, but persisted IDs are safer for changing data. Never make a fresh random key during render: changes to keys replace the matched component and may discard input. The useId hook (introduced later for accessibility IDs) is not a list-key generator.
Practice
Given three records with two identical display names and distinct IDs, render them with a stable key and show the ID through a separate prop. Then supply a duplicated ID and reject the dataset rather than guessing which record is correct.