TL;DR; Wrong elements from array get removed inside state array.
I've made a gridbuilder where people can add and remove grids from the screen. Currently people can add Papers with some random default text for testing purposes. The problem I have is that first of all when you hover over one element all the delete icons show up instead of just one for the current element. The other problem is that the wrong element gets deleted when user clicks the remove icon.
I have tried searching the web for removing the proper element from state array and found the following. Let's say you have an empty state:
this.state = {
action: []
}
to push to an array inside the state I use the following method:
updateAction = (newAction) => {
this.setState({action: [...this.state.action, newAction]});
};
now the question is how to remove the right element. I use the following:
removeElement = (id) => {
this.setState({action: this.state.action.filter((value) => value !== id)});
setTimeout(() => {
console.log(`removeElement function: ${this.state}`);
}, 400)
};
This might seem a little unclear please refer to this CodeSandbox.