Last active
September 10, 2020 14:59
-
-
Save mdovn/2de0ff2ff3b46bc46cdc23e727dc6ed0 to your computer and use it in GitHub Desktop.
React Icon component
This file contains 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 React from 'react'; | |
function importAll(r) { | |
return r.keys().map(r); | |
} | |
function getIcons() { | |
try { | |
return Object.fromEntries( | |
importAll( | |
require.context('./imgs', false, /\.(png|jpe?g|svg)$/) | |
).map((url) => [url.replace('/static/media/', '').split('.')[0], url]) | |
); | |
} catch { | |
return {}; | |
} | |
} | |
const icons = getIcons(); | |
export default function Icon({ name, ...rest }) { | |
return <img src={icons[name]} alt={name} {...rest} />; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment