reactjsfont-awesomereact-font-awesome

React.js:How to send Fontawsome icon as props?


I using this object as React.js component props. but after sent it, FontAwsome could not find passed icon

 {
      key: 'editButton',
      title: 'Edit',
      icon: 'faEdit',
      function: null,
      type: 'default ',
    },
    {
      key: 'deleteButton',
      title: 'Delete',
      icon: 'faTrash',
      type: 'danger',
      function: null,
    },

my component:

 <FontAwesomeIcon icon={item.icon} />

error:

index.js:1 Could not find icon {prefix: "fas", iconName: "faEdit"}

Solution

  • If you need to pass it as a string value, then you'll need to pre-register them first: https://fontawesome.com/how-to-use/javascript-api/methods/library-add

    See this also: Import all icons from Fontawesome

    Otherwise, you can explicitly pass them:

    import { faEdit } from '@fortawesome/free-solid-svg-icons';
    
    ...
     {
         key: 'editButton',
         title: 'Edit',
         icon: faEdit,
         function: null,
         type: 'default ',
     },