简体   繁体   English

如何在 object 中存储 React 组件

[英]How to store React component in object

I have an object of all apps:我有一个所有应用程序的 object:

 export const allApps: appInterface[] = [
  {
    id: uuidv4(),
    name: "Minesweeper",
    icon: minesweeperIcon,
    disabled: false,
  },
];

I want to add component property to each object like this:我想像这样向每个 object 添加组件属性:

 export const allApps: appInterface[] = [
  {
    id: uuidv4(),
    name: "Minesweeper",
    icon: minesweeperIcon,
    disabled: false,
    component: Minesweeper, //It is imported as import Minesweeper from ../Components/Minesweeper";
  },
];

And then I want to display all components in App.js:然后我想在 App.js 中显示所有组件:

allApps.forEach((app)=>{
  <div>{app.component}<div> // for eg: <div><Minesweeper/></div>
});

Is this possible to do?这可能吗?

Try as below code it should work.试试下面的代码,它应该可以工作。

allApps.map((app)=>{
const { component: Component } = app;
  return <Component />;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM