I am updating the state array as follows
const clearPath = () => {
const newGrid = grid.map((row) => {
return row.map((node) => {
if (node.isVisited) {
node.isVisited = false;
node.previousNode = null;
}
return node;
});
});
setGrid(newGrid);
//console.log(grid);
};
Rendering it as
return (
<div>
<div>
<button className="btn" onClick={() => clearPath()}>
<span>Clear</span>
</button>
</div>
<div className="arena">
{grid.map((row, i) => (
<div key={i} className="row">
{row.map((node, j) => {
const { row, col, isStart, isEnd, isWall, isVisited } = node;
return (
<Node
key={j}
col={col}
row={row}
node={node}
isEnd={isEnd}
isStart={isStart}
isWall={isWall}
isVisited={isVisited}
mouseIsPressed={mouseIsPressed}
onMouseDown={(row, col) => {
handleMouseDown(row, col);
}}
onMouseEnter={(row, col) => {
handleMouseEnter(row, col);
}}
onMouseUp={() => handleMouseUp()}
></Node>
);
})}
</div>
))}
</div>
</div>
);
Using the clearpath function I am able to modify the state but the app is not re-rendering. Why is that hapening? When does the re-render happen only if the state changes, right?
You are mutating state with node.isVisited = false;
If Node is a pure component it would cause none of the Node components to be re rendered.
return row.map((node) => {
if (node.isVisited) {
return {...node, isVisited : false,previousNode : null};
}
return node;
});