Ad
How To Map Through Component Right?
I'm a newbie and I start to learn react js. So now I have component which has svg icons inside:
export default class IconPick extends Component {
render() {
const { icon, size = 20, className, ...extraProps } = this.props;
let path;
let viewBox;
switch ( icon ) {
case 'envelope':
viewBox = '0 0 512 512';
path = 'M502.3...';
break;
case 'globe':
viewBox = '0 0 496 512';
path = 'M336.5...';
break;
case 'plus':
viewBox = '0 0 24 24';
path = 'M24...';
break;
}
if ( ! viewBox ) {
return null;
}
if ( ! path ) {
return null;
}
const iconClass = [ 'myicon', 'myicon-' + icon, className ].filter( Boolean ).join( ' ' );
return (
<SVG
aria-hidden
role="img"
focusable="false"
className={ iconClass }
xmlns="http://www.w3.org/2000/svg"
width={ size }
height={ size }
viewBox={ viewBox }
{ ...extraProps }
>
<Path d={ path } />
</SVG>
);
}
}
So everything working fine like that:
<IconPick icon={'globe'} />
So now I want to display every icon, but I don't know how to map right. I can do something like this, but this is not practical:
<div>
<IconPick icon={'globe'} />
<IconPick icon={'envelope'} />
<IconPick icon={'plus'} />
</div>
Can you help me, please? Thanks in advance.
Ad
Answer
JSX has a slight different syntax. Try this:
<div>
{["globe", "envelope", "plus"].map(icon => (
<IconPick key={icon} icon={icon} />
))}
</div>
More on that here
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad