I am trying to build a battleShip game using React.
I have a state like this inside my component, every coordinate has a list with two 'states', 'empty' if there is not a ship ('busy' if there is) in it and 'intact' the coordinate hasn't been hit ('hit' if it has).
this.state = {
status: {
A1: ['empty', 'intact'],
B1: ['busy', 'intact']
}
}
What I want to do is: When I click in a coord in the battleShip grid to shoot it, I want to set only the second element in the list to 'hit' and leave the first element without changes.
Something like this:
handleClick = (e) => {
const coord = e.target.id; //get the coord from the element clicked, like 'A1'
this.setState({ status: { coord[1]: 'hit' } });
}
So I want my new State to be :
this.state = {
status: {
A1: ['empty', 'hit'],
B1: ['busy', 'intact']
}
}
How do I write this.setState({ status: { coord[1]: 'hit' } })
in the correct way, so that my new State is exactly like above?