简体   繁体   English

如何从对象属性中检索 React 组件

[英]How to retrieve a React component from an object property

I've got a SliderMenu React component, which receives an array containing its elements.我有一个SliderMenu React 组件,它接收一个包含其元素的数组。 These elements are objects, and one of their properties is the icon that will be shown in the menu:这些元素是对象,它们的属性之一是将显示在菜单中的图标:

function SliderMenu({ options }) {
  return (
    <>
      <Drawer open>
        <List>
          {options.map((item) => {
            const { icon: ItemIcon } = item || {};
            return (
              <ListItem button key={`menuItem_${item.text.replace(/\s/g, '')}`}>
                <ListItemIcon>
                  <ItemIcon />
                </ListItemIcon>
                <ListItemText primary={item.text} />
              </ListItem>
            );
          })}
        </List>
      </Drawer>
    </>
  );
}

This component is called from an upper component (because I'd like to be able to create multiple SliderMenu if I needed them), which specifies the elements array and passes them as props:这个组件是从上层组件调用的(因为如果我需要的话,我希望能够创建多个SliderMenu ),它指定元素数组并将它们作为道具传递:

import React from 'react';
import { HomeRoundedIcon } from '@material-ui/icons/HomeRounded';
import SliderMenu from '../../components/SliderMenu';

function MainMenu() {
  const menuOptions = [
    {
      text: 'Home',
      icon: HomeRoundedIcon,
    },
  ];

  return (
    <SliderMenu options={menuOptions} />
  );
}

export default MainMenu;

The problem here is that whenever I try this code, ItemIcon is undefined, and I'm receiving the following error:这里的问题是,每当我尝试此代码时, ItemIcon都未定义,并且我收到以下错误:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

Check the render method of SliderMenu .检查SliderMenu的渲染方法。

However, if I try:但是,如果我尝试:

const menuOptions = [
    {
        text: 'Home',
        icon: <HomeRoundedIcon />,
    },
];

ItemIcon has a value, and I get the following error: ItemIcon有一个值,我收到以下错误:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象。

Check the render method of SliderMenu .检查SliderMenu的渲染方法。

I would like to be able to specify different menu elements with different icons and so on, depending on the context, and then use the common component to render it and have a consistent behaviour across all menus.我希望能够根据上下文使用不同的图标等指定不同的菜单元素,然后使用通用组件来呈现它并在所有菜单中具有一致的行为。

How can I define the icon in the object, using the icon component library, and then pass it via props to the SliderMenu so it can render it (without having to import all the icons and make a switch to see which icon has to render)?如何使用图标组件库定义对象中的图标,然后通过props将其传递给SliderMenu以便它可以呈现它(无需导入所有图标并进行切换以查看必须呈现哪个图标) ?

Change your import to a default import将您的导入更改为默认导入

import HomeRoundedIcon from '@material-ui/icons/HomeRounded'

or use a named import from @material-ui/icons path或使用来自@material-ui/icons路径的命名导入

import { HomeRounded as HomeRoundedIcon } from "@material-ui/icons"

You would also want to remove the default assignment and render the icon conditionally as this will throw an error when there is no icon您还需要删除默认分配并有条件地呈现图标,因为这会在没有图标时引发错误

const { icon: ItemIcon } = item

{ItemIcon && (
  <ListItemIcon>
    <ItemIcon />
  </ListItemIcon>
)}

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

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