0

I have an array of items, which are defined by a string and an icon component:

type MyItem = {
  Title: string;
  Icon: React.ElementType;
};

export const MyItems: MyItem[] = [
  { Title: "Inbox", Icon: InboxIcon },
  { Title: "Users", Icon: UserIcon },
  { Title: "Settings", Icon: SettingsIcon },
];

Now I want to display them within a component:

<>
    {MyItems.map((item, index) => (
      <h1>item.Title</h1>
      // render icon component here: item.Icon
    ))}
</>

How can I render the icon component (without changing the array structure)?

1 Answer 1

1

I'm guessing here that your icons are essentially React Components. If so then you can try out the code below:

<>
    {MyItems.map((item, index) => (
      <>
      <h1>item.Title</h1>
      {item.Icon}
      </>
    ))}
</>
Sign up to request clarification or add additional context in comments.

5 Comments

Already tried that and tried it again. I do get a blank screen if I put it whtin { }.
Have you tried removing ( after =>? I noticed it now that it's not needed. Try removing that bracket open and a ) on the 7th line.
As for your icons in the MyItems array, use <InboxIcon />, <UserIcon /> and <SettingsIcon /> for respective item.
Thanks that did work. I used React.ReactElement instead of React.ElementType and for the Array I used <InboxIcon />, <UserIcon /> and <SettingsIcon />.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.