React Developer Interview Question

Why does React need a key on list items, and what actually breaks if you use the array index?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Keys tell React which element in the new list corresponds to which one in the old list, so it can move, keep or remove DOM nodes and their state instead of matching purely by position. Array indexes only work for static lists. If items get reordered, inserted at the front or deleted, the index for a given item changes, React reuses the wrong node, and state such as input values lands on the wrong row.

Why interviewers ask this

It looks like a beginner question, but the answers separate people quickly. Most candidates say keys help performance and stop there. The interviewer wants to hear that keys are about identity and state preservation, not just speed, and that you can name the concrete failure mode with index keys. It is also a fast check on whether you have debugged a real list bug or only cleared a console warning.

How to structure your answer

  • Define a key as identity, not a performance hint.
  • Explain how the diff pairs old and new children by key.
  • Give the concrete index bug on a reordered or filtered list.
  • Say what you use as a key and what you avoid.

Example answer

Spoken example, first person

A key is how React answers the question, is this the same item I saw last render. Without one it pairs children by position, so when a row moves React thinks the content changed and patches the existing node instead of moving it. With a stable key it knows the item moved, keeps its DOM node and its state, and reorders. The index trap is that an index describes the position, not the item. I hit this on a table where each row had a checkbox and a notes field. Delete row two and every row below shifts up one index, so React pairs the wrong nodes and you end up with someone else's notes sitting in your row. It looks like a data bug and it is a key bug. So I use whatever stable id comes with the data, usually the database id. If there genuinely is not one, I generate an id when the item is created rather than during render, because generating in render gives you a fresh key every pass and remounts the whole list.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • When is using the array index as a key actually fine?
  • What happens if two siblings share the same key?
  • How would you deliberately reset a component's state using a key?

Related react developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot