Using indexes as keys

Keys should be stable, predictable, and unique so that React can keep track of elements.

Bad

In this snippet each element’s key will be based on ordering, rather than tied to the data that is being represented. This limits the optimizations that React can do.

  1. {todos.map((todo, index) =>
  2. <Todo
  3. {...todo}
  4. key={index}
  5. />
  6. )}

Good

Assuming todo.id is unique to this list and stable, React would be able to reorder elements without needing to reevaluate them as much.

  1. {todos.map((todo) =>
  2. <Todo {...todo}
  3. key={todo.id} />
  4. )}

@Reference: