Skip to content

Instantly share code, notes, and snippets.

@AmirHosseinKarimi
Last active January 16, 2023 23:57
Show Gist options
  • Save AmirHosseinKarimi/801931665a0845ccc25ed486431abf72 to your computer and use it in GitHub Desktop.
Save AmirHosseinKarimi/801931665a0845ccc25ed486431abf72 to your computer and use it in GitHub Desktop.
Dynamic load Material Design Icons in React
import React from "react";
import { Icon as MDIcon } from "@mdi/react";
class Icon extends React.Component {
render() {
let icon = require(`materialdesign-js/icons/${this.props.icon}`).default;
if (!icon) {
throw Error(`Could not find materialdesign-js/icons/${icon}`);
}
return <MDIcon path={icon} />;
}
}
export default Icon;
import React from "react";
import Icon from "./Icon";
ReactDOM.render(
<Icon icon="mdiAccount" />,
document.querySelector("#root")
);
@potty
Copy link

potty commented May 25, 2020

Does tree shaking work with this technique?

@AmirHosseinKarimi
Copy link
Author

I don't know.

@Carolinaribeirodacol
Copy link

But the generated file is too big

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment