【发布时间】:2022-01-13 18:44:29
【问题描述】:
今天我试图通过遍历数组来渲染一些 reactHooks。
我现在遇到的问题是在 tileMap 状态更新时不会发生的重新渲染。
tileMap 是一个对象数组(参见文章结尾)
状态发生变化,我可以用正确的值记录它,但仍然没有渲染 Tile 组件
function resetGame() {
console.log("yo");
let tileMap = []
//tileMap is updated here
//This is always reached
setTileMap(tileMap)
}
<div className="life2dgame-root">
{
tileMap ?
tileMap.map((row) => {
row.map((tile) => {
console.log(tile);
return <Tile isLife={tile.isAlive}/>
})
})
:
null
}
</div>
tileMap状态的类型
<{
loc: {height: number, width: number},
element: JSX.Element,
isAlive: boolean
}[][]>
【问题讨论】:
-
您的问题不清楚。尝试添加完整代码。
标签: reactjs typescript react-hooks render