Skip to content

Latest commit

 

History

History
26 lines (23 loc) · 765 Bytes

06.using-indexes-as-key.md

File metadata and controls

26 lines (23 loc) · 765 Bytes

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.

{todos.map((todo, index) =>
  <Todo
    {...todo}
    key={index}
  />
)}

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.

{todos.map((todo) =>
  <Todo {...todo}
    key={todo.id} />
)}

@Reference: