gpt4 book ai didi

javascript - 使用 lodash 在 React 中渲染网格

转载 作者:行者123 更新时间:2023-11-29 17:46:24 25 4
gpt4 key购买 nike

我很难为我的测试项目渲染一个简单的网格。不想手动创建网格,因为更大的网格不仅是一件苦差事,而且代码会很困惑,所以我想我应该为此使用 lodash。

但是,我似乎无法渲染网格,即使我在开发工具中检查它也是不可见的。有人可以指出我的错误吗?

如有必要,我也可以使用 lodash 以外的其他工具。

代码如下:

import React from 'react';
import _ from 'lodash';
import './game.css';


const GRID = [
[{x: 1, y:3}, {x:2, y:3}, {x:3,y:3}],
[{x: 1, y:2}, {x:2, y:2}, {x:3,y:2}],
[{x: 1, y:1}, {x:2, y:1}, {x:3,y:1}],
]
class Game extends React.Component {
renderGrid() {
return _.map(GRID, row =>{
_.map(row, cell =>{
return <div style={{height: 100 + 'px', width: 100+ 'px'}}> {cell.x}, {cell.y} </div>
})
})
}

render() {
return (
<div className="game">
{this.renderGrid()}
</div>
)
}
}

export default Game;

最佳答案

您不会返回内部 map 结果,一旦您返回它就会起作用

renderGrid() {
return _.map(GRID, row =>{
return _.map(row, (cell, index) =>{
return <div key={index} style={{height: 100 + 'px', width: 100+ 'px'}}> {cell.x}, {cell.y} </div>
})
})
}

Working codesandbox

关于javascript - 使用 lodash 在 React 中渲染网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com