So for a TictacToe game I would have a nested array in the state, like this:
field: [
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
],
Now I want to set a specific cell to X or O, but I couldn't get it to completely rerender. I have 2 function components and whatever I tried I could never get the cells to be updatet. The reducer correctly updates my state though. So should I write the reducer or change anything, so that each cell is rerendered if it is changed?
Code samples. Obviously there is more, but I believe this should be enough.
function Ttt_game(props){
return (
<div>
<table>
<tbody>
{
props.field.map((row, i) => (<Ttt_row row={row} key={i} row_index={i} />))
}
</tbody>
</table>
</div>
)
}
and
function Ttt_row(props){
return (
<tr>
{
row.map((cell, i) => (
<td key={i}>
<div onClick={() => insertIntoField({ x: row_index, y: i, symbol })}>
{cell}
</div>
</td>
))
}
</tr>
)
}
reducer:
case "ttt_insert":
let newField = state.field;
newField[action.payload.x][action.payload.y] = action.payload.symbol;
return {
...state,
field: newField
};