1

Got a Warning: Each child in a list should have a unique "key" prop. I cant figure out which key I need to use.

<td className="banana-td">
                    {todos.map((todo, index) => (
                        <BananaBullet
                            key={index.id}
                            value={todo.date}
                            completed={todo.completed}
                            onClick={() => 
           toggleTodo(todo.id)}
                        />
                    ))}
                </td>

                <td className="task-td">
                    {todos.map((todo, index) => (
                        <TodoContainer
                            key={index.id}
                            text={todo.text}
                            completed={todo.completed}
                            toggleTodoItem={() => 
          toggleTodo(todo.id)}
                        />
                    ))}
                </td>
                <td>
                    {todos.map((todo, index) => (
                        <DeadlineList
                            key={index.id}
                            value={todo.date}
                            completed={todo.completed}
                            onClick={() => 
                 toggleTodo(todo.id)}

I red the react guidelines, but it doesnt help me understand how to use it in my case

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
FilipZafran
  • 315
  • 1
  • 5
  • 14

3 Answers3

2

index is a number, not an object. Just index is enough.

key={index}
kind user
  • 40,029
  • 7
  • 67
  • 77
2

In your code, you have mentioned:

key={index.id}

This will not work because, key is a number / integer. All you need to do is:

key={index}

If I understand what you're trying to do, then you should be doing:

key={todos[index].id}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

It looks like your todo object has a unique field on it (id), so you can put in todo.id as the key. This is far better than using an index value, since React may get confused with ordering during state updates. Do this for the BananaBullet, TodoContainer, and DeadlineList. For example:

<BananaBullet
  key={todo.id} // change here
  value={todo.date}
  completed={todo.completed}
  onClick={() => toggleTodo(todo.id)}
/>
Ross Sheppard
  • 850
  • 1
  • 6
  • 14