reactjs 基于对象属性号React生成组件

bvjveswy  于 2023-01-08  发布在  React
关注(0)|答案(1)|浏览(102)

我试图根据对象文件的属性(一个数字)生成组件。每次我尝试时,我都会收到一个错误消息,说当前父组件得到的子组件太多。
假设files属性为5,有人知道如何在Grid项中为每个文件夹生成5个div吗?

{folders.map((folder, index) => (
        <Grid item key={index} xs={4}>
          <CustomItem>
            <div style={{ display: "flex", flexDirection: "column" }}>
              <CustomTypography>{folder.title}</CustomTypography>
              {/* <CustomTypography>{deck.message}</CustomTypography> */}
            </div>
            <Button
              onClick={() => handleDelete(folder._id)}
              sx={{
                position: "absolute",
                top: "10px",
                right: "0px",
                width: "10px",
              }}
            >
              <DoDisturbOnIcon />
            </Button>
            <Button
              onClick={() => handleAddFile(folder._id)}
              sx={{
                position: "absolute",
                top: "10px",
                left: "0px",

                borderLeftColor: "0px",
                width: "10px",
              }}
            >
              <AddCircleIcon />
            </Button>
          </CustomItem>
        </Grid>
      ))}

我基本上是这样做的:

{for (let i = 0; i < folder.files + 1; i++) {
return <CustomItem>
}}
63lcw9qa

63lcw9qa1#

您可以使用

{ 
  Array.from({length: folder.files }).map((_, i) => <CustomItem key={i} /> )
}

相关问题