Programming Languages

React List Keys: Why Position Is Not Always Identity

SoftwareJobs.io editorial29 November 20262 min read

A list item can move while still representing the same thing. React uses keys to identify items among their siblings. Choosing a key based only on position can cause confusing behaviour when items are inserted, removed or reordered.

Use an editable list example

Imagine three tasks with their own stable IDs and a text input in each row. A user edits the second task, then another task is inserted at the top. The previous positions have shifted. You want the row’s identity to follow the task, not whichever item now happens to be second.

{tasks.map(task => (
  <TaskRow key={task.id} task={task} />
))}

React’s list guide recommends a stable key based on the data. Random values generated during rendering also fail to preserve identity. The key is a React hint and is not passed to the child as an ordinary prop; pass a separate ID if the component needs it.

Try the behaviour yourself

  • Create a small local list with stable IDs.
  • Edit one row and insert another before it.
  • Reorder the rows and observe the input behaviour.
  • Explain why the expected identity should remain the same.

Explain the reason in an interview

Do not stop at “React needs a key to remove a warning”. Describe what each row represents and how its identity survives a change in ordering. For Malta frontend interviews, a small behaviour-based example is easier to assess than repeating a rule without understanding what it protects.

Sources and further reading

Explore current technology jobs in Malta →

PUT YOUR IDEAS INTO ACTION

What’s your next move?

Explore software, cloud and data roles that fit your skills and ambitions.

Explore opportunities
KEEP EXPLORING

More ideas for your next chapter