为什么即使状态已更改,应用程序也不会重新呈现?

时间:2020-05-23 07:01:43

标签: reactjs react-state-management

我正在如下更新状态数组

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);
  };

将其呈现为

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>
);

使用clearpath函数,我可以修改状态,但应用程序未重新渲染。为什么会发生这种情况?仅在状态更改时才进行重新渲染,对吧?

1 个答案:

答案 0 :(得分:1)

您正在使用(select * from toys where color = 'Yellow' LIMIT 1) UNION ALL (select * from toys where color = 'White' LIMIT 1) 改变状态,如果Node是纯组件,则将不会导致任何Node组件被重新渲染。

node.isVisited = false;