1

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"}
Rohullah Rajaee Rad
  • 571
  • 2
  • 9
  • 33

3 Answers3

1

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 ',
 },
MorKadosh
  • 5,846
  • 3
  • 25
  • 37
1

I solved this problem by a simple condition. calling components here using

<componentName theIcon="faCrown" />

and while rendering componentName , i used

 <FontAwesomeIcon icon={props.theIcon=== "faCrown" ? faCrown: faLink} />

You need to import those icons as well

Shuhad zaman
  • 3,156
  • 32
  • 32
0

Here is how I passed an icon as prop in a custom input

// custom Input with Tailwind Css and formik
import { FaUser } from 'react-icons/fa';

// inside Input component  

<label 
     htmlFor={name} 
     className='block 
     text-sm font-medium 
     text-gray-900 
     dark:text-white'>
 Full Name
</label>

<div className='relative'>
   {icon && (
            <div className='flex 
                   absolute inset-y-0 
                   left-0 pl-3 items-center 
                   pointer-events-none'>
               {icon} // <== displaying the passed icon into Input
            </div>
  )}
  <input type='text' {...field} {...props} /> //<-- formik
</div>

// You call it where you want it as follow
<Input 
      type='text'
      name='name'
      label='Full Name'
      // Passing icon below as prop
      icon={<FaUser className={iconCls} fill='currentColor' />} 
/>  

Result enter image description here

DiaMaBo
  • 1,879
  • 1
  • 18
  • 18