I have a class component. I've added this line in the constructor:
this.handleChangedCells = this.handleChangedCells.bind(this);
I register a SocketIO listener in componentDidMount and call a function like so
socket.on("changedCells", this.handleChangedCells);
I am receiving the data as I console.log it once I receive it. The problem is that the function calls this.setState and it does not update the component. I have
handleChangedCells(data) {
console.log(data);
let new_board = this.state.board;
data.map(arr => {
arr.map(obj => {
new_board[obj.ind[0]][obj.ind[1]] = {
color: obj.color,
alive: obj.alive
};
});
});
this.setState({ board: new_board });
}
The component that I have is as follows:
render() {
return (
<div className="boardContainer" style={container_style}>
{this.state.board.map((row, r_ind) => {
return (
<div className="row" key={r_ind}>
{row.map((col, c_ind) => {
return (
<Cell
key={[r_ind, c_ind]}
cell_row_index={c_ind}
cell_col_index={r_ind}
socketProps={socket}
colorProps={this.state.color}
clickedColorProps={col.color}
aliveProps={col.alive}
/>
);
})}
</div>
);
})}
</div>
);
}
As you can see, the component depends on the state which I update however, the component is not updated... Any idea of what is happening here?
EDIT: Here's a link to my project https://github.com/Dobermensch/ExpressTest.git The file in question is /client/components/Game.js
Edit 2: board is an array of arrays containing objects {color: [x,y,z], alive: boolean} and I am getting the changed cells, (dead cells) and marking them as dead in the board so ideally I would go the the dead cell's index and mark it as dead by board[row][col] = {color: [x,y,z], alive: false}
Edit 3: data structure of board is [[{}.{},{}],[{},{},{}]] and I am changing the properties of objects within the array
let new_board = this.state.board;Nownew_board === this.state.boardsothis.setState({ board: new_board });would be the same asthis.setState({ board: this.sate.board });and would cause React to not detect any changes. We don't know whatthis.state.boardis so can't really give you an answer as to how to set it. - HMRlet new_board = JSON.parse(JSON.stringify(this.state.board));and then making the changes in new_board then setting new_board as state but still no luck - Ayudh