Created
November 21, 2022 01:44
-
-
Save johncovv/93c9112df806cdd03ec68897c87ddac9 to your computer and use it in GitHub Desktop.
Load react icon using tag name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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