Skip to content

Latest commit

 

History

History
35 lines (31 loc) · 586 Bytes

06.using-indexes-as-key.md

File metadata and controls

35 lines (31 loc) · 586 Bytes

Using indexes as keys

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

@Reference:

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
  }
}