【发布时间】:2021-03-19 18:10:06
【问题描述】:
我想根据一个数字常量来渲染组件。
使用不会有问题的数组,但我没有解决常量的方法。
const numberOfItems = 4;
return (
{/* The for loop should be here, with which 4 <Grid> components should then be rendered */}
<Grid item xs={12} sm={6}>
<TextField
required
id="quadrant-one"
name="quadrant-one"
label="Quadrant 1"
fullWidth
/>
</Grid>
)
【问题讨论】:
-
代替 for 循环,使用 lodash 库方法次。 jsx内部:
{times(4, (idx) => <YourComponent />)} -
我要结束这个问题,因为答案在数组的创建方式上会有所不同(这本身就是一个老问题),映射最终是相同的(返回一个带有
key属性的 JSX如 React 文档中所述)。
标签: reactjs typescript react-redux jsx react-component