Skip to content

Instantly share code, notes, and snippets.

@mdovn
Last active September 10, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdovn/2de0ff2ff3b46bc46cdc23e727dc6ed0 to your computer and use it in GitHub Desktop.
Save mdovn/2de0ff2ff3b46bc46cdc23e727dc6ed0 to your computer and use it in GitHub Desktop.
React Icon component
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