使用.map() react的未定义变量



我正在重新访问学校课程作业的旧项目,但我一直得到一个未定义的网格变量生成我的网格的错误。

我想循环的行和列,我想要的属性存储在一个数组称为网格。然后,我想用.map()显示数组中行和列的节点。

我很新的反应虽然,似乎找不到为什么我不能访问渲染()中的网格数组。

class PathfinderGrid extends Component {
constructor(props) {
super(props)
this.state = {
grid: [],
};
}

componentDidMount() {
const grid = createGrid();
this.setState({ grid });
//console.log(this.state.grid)
}
render() {
return (
<div>
{grid.map((grid) => {
<div>{ grid }</div>
})}
</div >
);
}
}

const createGrid = () => {
let grid = [];
for (let row = 0; row < 15; row++) {
let currentRow = [];
for (let col = 0; col < 50; col++) {
currentRow.push();
}
grid.push(currentRow);
//console.log(grid);
}
return grid;
};
export default PathfinderGrid;

既然你使用的是类组件,我相信你需要写

{this.state.grid.map((grid) => {
return <div>{ grid }</div>
})}

最新更新