Answer the question
In order to leave comments, you need to log in
Why does the method work incorrectly with adding unique keys?
Created a card with the ability to add tasks. When adding tasks, the method starts adding them incorrectly (duplicates them), and with each addition more and more.
Moreover, if in the renderTasks method in return
return (
<p className="task" key={new_current_id} >{item}</p>
)
return (
<p className="task">{item}</p>
)
Answer the question
In order to leave comments, you need to log in
Because it key
must be a unique value, and you use the same value for each element of the list. The easiest and most primitive way to shut up React so that it does not swear at the absence of a key is to do something like this:
taskList.map(function(item, idx) {
return (
<p className="task" key={idx} >{item}</p>
)
})
taskList = [
{id: 1, objective: 'Buy milk'},
{id: 2, objective: 'Learn React'},
]
taskList.map(task => (<div key={task.id}>{task.objective}</div>))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question