Keys Matter
Part of the Fundamentals section of Coddy's React journey — lesson 36 of 42.
Every element returned from map needs a key, a stable, unique label for that item:
{todos.map((todo) => <li key={todo.id}>{todo.text}</li>)}Why? When the array changes, React compares the new list with the old one. Keys tell it which item is which, so it moves elements instead of rebuilding them, keeping state, focus and animations intact.
Best key: a real id from your data. Acceptable: any value unique and stable per item (like our fruit names). Last resort: the array index. It breaks reordering, because position isn't identity.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
7Conditions & Lists
Ternary RenderingShow with &&Render a ListKeys MatterFilter, Then MapRecap: Conditions & Lists2JSX Expressions
Curly BracesMath in BracesBraces in AttributesCalling Functions in JSXRecap: JSX Expressions5State & Events
What Is State?Click CounterCount in StepsToggle with StateIndependent StateRecap: State & Events8Project: Profile Card
Step 1: Card StructureStep 2: Card with PropsStep 3: Like ButtonStep 4: Follow Toggle