I want to delete an object from an array by a generated object id.
I've already tried multiple solutions on this and nothing is working.
Handler function:
handleDeleteTask = id => {
const task = this.state.tasks.find(i => {
return i.id === id;
});
const newTasks = this.state.tasks.splice(task, 1);
this.setState({ tasks: newTasks });
};
JSX:
{this.state.tasks.map(task => {
return (
<Task
key={task.id}
task={task.text}
title={task.title}
deleteTask={() => this.handleDeleteTask(task.id)}
/>
);
})}
this.state.tasks.splicewill mutate the state, instead make a copyconst newTasks = [...this.state.tasks]then apply the splice on yournewTasksvariable