I try to update a state in a location object.
somehow the setState isn't working for me.
The console.log does return the newName correctly.
I don't see directly where my fault is. Can someone tell me where my error is?
state = {
locations: [
{name:"name1", address:"address1", locationSelected:false},
{name:"name2", address:"address2", locationSelected:false}
]
}
selectLocationHandler = (id) => {
let theLocations = [...this.state.locations];
theLocations[id] = {...theLocations[id], name:"newName!!!!"};
console.log(theLocations[id].name + "testtest");
this.setState({theLocations}, () => {
console.log(this.state.locations[id].name + " it worksss");
});
}
selectLocationHandlerfunction? Are you trying to handle the new item in the array?