Brief description
So thanks to the svg-to-react-cli script I have 73 components for my icons.
Now, if I want to call these icons, I can individually call each one I want to use. But this is not what I want.
What I want is to call only one component where I add a 'name' value to.
e.g.: < Icon name='Name of one of the 73 components I wish to call' />
My question
How do I loop through multiple components inside a directory, and return them inside a switch case?
This is how all my 73 components inside the directory look like
import React from 'react';
export default function NAMEOFICON(props) {
return (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width={100} height={100} {...props}>
...CONTENT OF SVG...
</svg>
);
}
This is what I have
import React from 'react';
import ALLCOMPONENTS from 'DIRECTORY WITH COMPONENTS'
// HERE I WANT TO LOOP THROUGH COMPONENTS AND ADD THEM IN THIS SWITCH
const getPath = (name, props) => {
switch (name) {
case 'NAME OF THE COMPONENT':
... HERE COMES CONTENT OF COMPONENT...
default:
return <path />;
}
};
// HERE I CREATE A CONST TO LATER CALL THE DESIRED COMPONENT BY NAME
const Icon = ({
name = '',
style = {},
fill = 'currentColor',
viewBox = '',
width = '20px',
className = 'icon icon' + name,
height = '20px'
}) => (
{NAME OF COMPONENT({ fill })}
);
export default Icon ;