我正在try 制作一个可重用的InfoBlock组件.该组件呈现一个项目列表.每件物品都有标签、图标和价值.问题是我不知道如何将可用的INFO_BLOCK_ITEMS映射到数据对象,并只呈现数据对象中存在的项.

所有可用标签和图标的完整列表如下所示:

const INFO_BLOCK_ITEMS = {
  author: {
    label: "Author",
    icon: AccountTreeIcon
  },
  date: {
    label: "Created",
    icon: FaceRetouchingNaturalIcon
  },
  rawMedias: {
    label: "RAW Medias",
    icon: InsertDriveFileOutlinedIcon
  },
  face: {
    label: "Faces",
    icon: ScheduleOutlinedIcon,
    faceAction: true,
  },
  s3Source: {
    label: "S3 Source",
    icon: AcUnitIcon
  }
};

我传递给InfoBlock组件的数据对象和数据类型(对于另一个页面,数据 struct 将不同,但它将包含INFO_BLOCK_ITEMS中的键:

const DATASET = {
  author: "extrauser@site.com",
  date: 1669208819,
  rawMedias: "Video name 1, Video name 2, Video name 3",
  face: ""
};

<InfoBlock data={DATASET} type={"dataset"} />

对于数据对象中的每个键,结果应该是这样的列表:

  <Stack>
    <AccountTreeIcon />
    <Stack>
      <Typography>Author</Typography>
      <Typography>extrauser@site.com</Typography>
    </Stack>
  </Stack>

这里有一个硬编码的Codesandbox:https://codesandbox.io/s/info-block-forked-0bwrze?file=/src/InfoBlock.tsx

推荐答案

  1. 您不需要将类型传递给InfoBlock组件.

  2. 原始媒体类型中的数据应为数字.

  3. 使用图标作为Reaction组件.

希望能有所帮助.

类型:

export type Dataset = {
  author: string;
  date: number;
  rawMedias: string;
  face: string;
};

export type RawMedia = {
  s3Source: string;
  author: string;
  date: number;
  face: string;
};

InfoBlock组件:

const INFO_BLOCK_ITEMS = {
  author: {
    label: "Author",
    icon: <AccountTreeIcon />
  },
  date: {
    label: "Created",
    icon: <FaceRetouchingNaturalIcon />
  },
  rawMedias: {
    label: "RAW Medias",
    icon: <InsertDriveFileOutlinedIcon />
  },
  face: {
    label: "Faces",
    icon: <ScheduleOutlinedIcon />,
    action: () => console.log("If no data then button renders")
  },
  s3Source: {
    label: "S3 Source",
    icon: <AcUnitIcon />
  }
};

interface IInfoBlockProps {
  data: Dataset | RawMedia;
}

function InfoBlock({ data }: IInfoBlockProps) {
  return(
    <Stack gap={"20px"}>
      {
        Object.keys(data).map((key, _index) => {
          const infoBlockItem = INFO_BLOCK_ITEMS[key];
          return (
            <Stack key={_index} direction={"row"} gap={"10px"}>
              {infoBlockItem.icon}
              <Stack direction={"row"} gap={"20px"}>
                <Typography>{infoBlockItem.label}</Typography>
                <Typography>{data[key]}</Typography>
              </Stack>
            </Stack>
          );
        })
      }
    </Stack>
  )
}

应用程序组件:

const DATASET = {
  author: "extrauser@example.com",
  date: 1669208819.837662,
  rawMedias: "Video name 1, Video name 2, Video name 3",
  face: ""
};

const RAW_MEDIA = {
  s3Source: "https://example.com",
  author: "extrauser@example.com",
  date: 1669208819.837662,
  face: "Some face"
};

function App() {
  return (
    <div>
      <InfoBlock data={DATASET} />
      <InfoBlock data={RAW_MEDIA} />
    </div>
  );
}

Reactjs相关问答推荐

无法在列表中看到文本

react -在选项中 Select 我想要显示和图像.但当我 Select 该选项时,我希望控件仅显示该选项的文本部分

为什么我的react虚拟化列表不显示滚动条,除非我确保高度低于rowCount*rowHeight?

为多租户SSO登录配置Azure AD应用程序

Reaction Ploly:如何在多个子情节中共享zoom 状态

React中的useEffect钩子

不同步渲染

禁止直接访问Next.js中的页面,同时允许从应用程序内部访问

阻止组件更新的多分派Redux Thunk或setState是否有任何问题

为什么我的react 简单计时器项目不起作用?

React 错误无法读取未定义的属性(读取id)#react

React设置上下文并进行导航

如何在不使用 Axios 的情况下将图像文件从 React.js 发送到 Spring Boot Rest API?

在react 中单击父级中的按钮时如何将数据从子级发送到父级?

运行 npm run build 出现问题

如何防止开发人员使用 ESLint 在 React 项目中使用特定的钩子?

添加 React/Redux 组件模板的 VS Code 扩展

Storybook - 无法解析generated-stories-entry.cjs/字段browser不包含有效的别名配置

如何在 Cypress E2E 的站点上测试react 组件?

当我在其中传递 2 个箭头函数时,为什么我的 onClick 事件不适用于 useState