splice() mutates the original array and should be avoided. Instead, one good option is to use filter() which creates a new array so does not mutates the state. But I used to remove items from an array using splice() with spread operator.
removeItem = index => {
const items = [...this.state.items];
items.splice(index, 1);
this.setState({ items });
}
So in this case when I log items
changes but this.state.items
stays unchanged.
Question is, why does everyone use filter
instead of splice
with spread
? Does it have any cons?