Last active
          January 16, 2023 23:57 
        
      - 
      
- 
        Save AmirHosseinKarimi/801931665a0845ccc25ed486431abf72 to your computer and use it in GitHub Desktop. 
    Dynamic load Material Design Icons in React
  
        
  
    
      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 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; | 
  
    
      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 React from "react"; | |
| import Icon from "./Icon"; | |
| ReactDOM.render( | |
| <Icon icon="mdiAccount" />, | |
| document.querySelector("#root") | |
| ); | 
I don't know.
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
  
            
Does tree shaking work with this technique?