Skip to content

Latest commit

 

History

History
34 lines (31 loc) · 582 Bytes

06.using-indexes-as-key.md

File metadata and controls

34 lines (31 loc) · 582 Bytes

Using indexes as keys

TL;DR: Generate a unique id for every item and use it as key when rendering the list.

Bad

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

Better

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

Even better

var shortid = require('shortid');
function createNewTodo(text) {
  return {
    completed: false,
    id: shortid.generate(),
    text
  }
}

@Reference: