![](/img/trans.png)
[英]Component does not re-render when State is updated using react, redux and immer
[英]React state not changing using immer package
所以我非常困惑,基本上当我点击它们时瓷砖应该变成灰色,但事实并非如此。 我很确定我这样做是正确的,除非我首先设置类型的方式缺少某些东西。 What's even weirder is sometimes when I go to manually change the state through the extension, all of a sudden (just for the single tile I manually changed the state for), the onClick works perfectly. 任何帮助表示赞赏,谢谢!
import React, { useEffect, useState } from 'react';
import produce from 'immer';
import { Node } from './astar';
export default function Grid(): JSX.Element {
const [grid, setGrid] = useState<Node[][]>([]);
const numCols = 50;
useEffect(() => {
let tempGrid: Node[][] = [];
for (let i = 0; i < numCols; i++) {
tempGrid.push([]);
}
for (let i = 0; i < tempGrid.length; i++) {
for (let j = 0; j < tempGrid.length / 2; j++) {
let node = new Node(i, j, false);
tempGrid[i][j] = node;
}
}
setGrid(tempGrid);
}, []);
return (
<div style={{ display: 'grid', gridTemplateColumns: `repeat(${numCols}, 20px)` }}>
{grid.map((rows, i) => {
return (
<div key={i}>
{rows.map((cols, j) => {
return (
<div
key={j}
onClick={() => {
const newGrid = produce(grid, (tempGrid) => {
tempGrid[i][j].isWall = !tempGrid[i][j].isWall;
});
setGrid(newGrid);
console.log(grid[i][j]);
}}
style={{ width: '20px', height: '20px', border: '1px solid black', backgroundColor: grid[i][j].isWall ? '#A9A9A9' : '#FFFFFF' }}
></div>
);
})}
</div>
);
})}
</div>
);
}
节点 Class
class Node {
x: number;
y: number;
isWall: boolean;
constructor(x: number, y: number, isWall: boolean) {
this.x = x;
this.y = y;
this.isWall = isWall;
}
}
export { Node };
即使我点击它仍然不会改变 state。
您的Node
class 需要标记为immerable
以使其与 Immer 兼容:
import {immerable} from "immer"
class Node {
[immerable] = true; //Add this line
x: number;
y: number;
isWall: boolean;
constructor(x: number, y: number, isWall: boolean) {
this.x = x;
this.y = y;
this.isWall = isWall;
}
}
export { Node };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.