React · Frontend system thinking
Why are keys important when rendering lists?
Short answer
Keys give list items a stable identity. React uses them to match elements across renders, preserve component state and avoid unnecessary remounts when items move.
During reconciliation, React compares siblings. Without keys, it relies on position. With keys, it can tell that an item moved rather than that every item changed.
function TodoList({ todos }) {
return (
<ul>
{todos.map((todo) => (
<TodoItem key={todo.id} todo={todo} />
))}
</ul>
);
}Why index keys fail
todos.map((todo, index) => (
<TodoItem key={index} todo={todo} />
));- Keys must be unique among siblings, not globally
- Keys are not passed to the component as a prop
- Use stable IDs from the data, not random values created during render
- Keys are also useful to reset a component intentionally when the identity changes
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why can index keys cause bugs when items reorder?
- 2Are keys passed to the child component?
- 3Can you use a key to reset component state?
- 4What makes a good key?
Say it in the interview
Keys give siblings a stable identity during reconciliation. React uses them to reuse the right DOM and component state when a list changes. Index keys break when items reorder or are inserted, because the index no longer identifies the same item. I use stable IDs from the data. Keys are scoped to siblings and are not passed to the component as a normal prop.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live React mock interview and get scored feedback on depth, clarity and confidence.