I have a state array that I want to perform actions on and then update the state, but it seems like I'm missing a deep copy somewhere and the state get updated without setState which causes me problems (I know that because I see the state gets updated even without the setState line) .
const itemsToRemove = ['Pepsi', 'Fanta'];
let temp = [...this.state.categories];
let categoryToUpdate = temp.find((category) => category.name == 'drinks');
categoryToUpdate.items = categoryToUpdate.items.filter((item) => {
return !itemsToRemove.includes(item);
});
//this.setState({categories: temp})
After those lines I would like categories state in object with "drinks" name to still include the items in itemsToRemove because I did not update the state but they still get removed from the state.
categories state's structure is as follows:
categories:[{name: String, items: Object}...]