I need to import a icon dynamically with the props will receive in the component and return the icon compatible with this, i have a question about this:
import { useEffect, useState } from 'react';
export default function Icon({ libraryName, iconName }) {
const [icon, setIcon] = useState(null);
async function getIcon() {
const path = `../../../node_modules/react-icons/${libraryName}`;
const returnedIcon = await import(path);
if (returnedIcon) setIcon(returnedIcon[iconName]);
}
useEffect(() => {
if (iconName) {
getIcon();
} else {
setIcon(null);
}
}, []);
return <span>{icon}</span>;
}
So, if you call this component for example with this
<Icon libraryName="md" iconName="mdMonitor" />
This work!
But, maybe the const path can be change with another path?
I think that exist better way to do this, if any can help me i would be grateful.