wmira / react-icons-kit

React Svg Icons
https://react-icons-kit.vercel.app/
MIT License
370 stars 45 forks source link

how to conditional import and render? #30

Closed zilahir closed 6 years ago

zilahir commented 6 years ago

Hey!

I have the following component rendering in a loop:

<ListItem isBeta={menuItem.isBeta} key={menuItem.id} title={menuItem.title} id={menuItem.id} icon={menuItem.icon} />

the menuItem.icon is returned by an API.

Let's assume that props.icon = "home"

Then, in the definition of ListItem component i have the following:

.
.
import { home } from 'react-icons-kit/fa/home'; 
return (  
        <li key={props.id} className={isBetaClass}>
            <Icon icon={props.icon} />
            {props.title}
        </li>
    );

but this is not working, i can't render the ion based on a received props. Any idea how to do this?

thanks!

wmira commented 6 years ago

@zilahir create a map of key and icon e.g.

import { home } from 'react-icons-kit/fa/home';
import { another } from 'react-icons-kit/fa/another';
const ICONS = {
  home: home,
  someKey: another
}
return (  
        <li key={props.id} className={isBetaClass}>
            <Icon icon={ICONS[props.icon]} />
            {props.title}
        </li>
    );
wmira commented 6 years ago

Closing this now