How to delete object from array using object property - React
Two issues there:
You're seeming to try to direct modify
this.state.tasks
. It's important not to do that, never directly modifythis.state
or any object on it. See "Do Not Modify State Directly" in the React documentation for state.You're passing an object to
setState
that is derived from the current state. It's important never to do that, too. :-) Instead, passsetState
a function and use the state object it passes you when calling that function. From "State Updates May Be Asynchronous" in the documentation:Because
this.props
andthis.state
may be updated asynchronously, you should not rely on their values for calculating the next state... [Instead]...use a second form ofsetState()
that accepts a function rather than an object.(my emphasis)
I figure your remove
on an array was intended to be hypothetical, but for the avoidance of doubt, arrays don't have a remove
method. In this case, the best thing to do, since we need a new array, is to use filter
to remove all entries that shouldn't still be there.
So:
deleteTask(taskToDelete) {
this.setState(prevState => {
const tasks = prevState.tasks.filter(task => task.name !== taskToDelete);
return { tasks };
});
}
You could simply filter the array :
this.setState(prevState => ({
tasks: prevState.tasks.filter(task => task.name !== 'taskToDelete')
}));
Also when updating based on this.state
, its better to use the function form because setState
is async.
You can use filter to remove one object from an array following the immutable pattern (filter will create a new array) :
deleteTask(taskToDelete) {
const newTaskArray = this.state.tasks.filter(task => task.name !== taskToDelete);
this.setState({ tasks: newTaskArray });
}
Edit : codepend of the solution : https://codepen.io/Dyo/pen/ZvPoYP