Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Element type is invalid: expected a string (for built-in components) or a class/function. while trying to render a component

`
Unhandled Runtime Error
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 DashboardCards.

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Im trying to have icons and labels in a common file constants/drawer

import DashboardIcon from "@mui/icons-material/Dashboard";

export const MainMenuItems = [
  {
    label: "Dashboard",
    icon: <DashboardIcon />,
    redirectLink: "/",
  },
];

and in the main.js file importing them and trying to map them.

Here i want to pass some props to Icon like <Icon size="small"/>.

import { MainMenuItems } from "../constants/drawer.js";
const MainContainer=()=>{

return (
<>

     {MainMenuItems.map(({label,icon:Icon}) => {
        
          return (
              <div>
<Icon size="small"/>

              </div>
            )
          );
        })}

</>
)

}

>Solution :

The Icon variable holds the component, you just need to execute it in the main.js file.

import { MainMenuItems } from "../constants/drawer.js";
const MainContainer = () => {
    return (
        <>
            {MainMenuItems.map(({ label, icon: Icon }) => {
                return <div>{Icon}</div>;
            })}
        </>
    );
};

UPDATE

If you want to pass props to the component, I suggest you to just save the component reference to the icon property.

import Drawer from "@mui/material/Drawer";

export const MainMenuItems = [
  {
    label: "Dashboard",
    icon: DashboardIcon,
    redirectLink: "/",
  },
];

main.js

import { MainMenuItems } from "../constants/drawer.js";
const MainContainer = () => {
    return (
        <>
            {MainMenuItems.map(({ label, icon: Icon }) => {
                return <div><Icon size="small" /></div>;
            })}
        </>
    );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading