I have react component named IconRenderer and I'm passing 2 props named iconName and iconPath. What the IconRenderer do is it imports react-icon module using lazyload by taking the iconPath and iconName
The following is my IconRenderer component
type Props = {
iconName: string;
iconPath: string;
};
const IconRenderer = (props: Props) => {
const { iconName, iconPath } = props;
const path = `react-icons/${iconPath}`
const Icon = lazy(async () => {
const module = await import(path);
return { default: module[iconName] };
});
console.log(Icon);
return (
<Suspense fallback={<div>Loading...</div>}>
<IconContext.Provider value={{ size: "3em" }}>
<Icon />
</IconContext.Provider>
</Suspense>
);
};
export default IconRenderer;
I'm using this IconRenderer component in the APP.tsx as below
<IconRenderer iconName="FaAccessibleIcon" iconPath="fa" />
When importing the module, it says the following error
Cannot find module 'react-icons/fa'
Anyhow when importing the module by directly passing the path without assigning it to a variable as below, it works
const Icon = lazy(async () => {
const module = await import(`react-icons/fa`);
console.log(module)
return { default: module[iconName] };
});
What i want is to import the module by taking the variable path
This ensures that the dynamic import uses the correct path at runtime. Storing the path in a variable (
const path = react-icons/${iconPath}) may lead to issues, especially if the dynamic import is unable to resolve the path correctly in certain situations.