Skip to content

Instantly share code, notes, and snippets.

@johncovv
Created November 21, 2022 01:44
Show Gist options
  • Save johncovv/93c9112df806cdd03ec68897c87ddac9 to your computer and use it in GitHub Desktop.
Save johncovv/93c9112df806cdd03ec68897c87ddac9 to your computer and use it in GitHub Desktop.
Load react icon using tag name
import { IconBaseProps } from 'react-icons/lib';
import loadable from '@loadable/component';
interface IPropsIcon {
nameIcon: string;
propsIcon?: IconBaseProps;
}
export default function LoadableIcon({ nameIcon, propsIcon }: IPropsIcon): JSX.Element {
const lib = nameIcon
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
.split(' ')[0]
.toLocaleLowerCase();
const ElementIcon = loadable(() => import(`react-icons/${lib}/index.js`), {
resolveComponent: (el: JSX.Element) => el[nameIcon as keyof JSX.Element],
});
return <ElementIcon {...propsIcon} />;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment