I am using FontawesomeIcon in a react js project and the names of the icons are coming from the database. I want to import the icons coming from the database from @fortawesome/free-solid-svg-icons dynamically
import React, { Component } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {faImage} from "@fortawesome/free-solid-svg-icons";
export class Features extends Component {
render() {
return (
<div id="features" className="text-center">
<div className="container-fluid">
<div className="features-listing">
{this.props.data.map((item, index) => (
<div key={`${index}`}>
{" "}
<FontAwesomeIcon icon={item.icon} />
<h3>{item.title}</h3>
</div>
))}
</div>
</div>
</div>
);
}
}
export default Features;
There is actually a better way without having to iterate all icons (@Andrei Rosu's solution), you can export the whole brand and solid packages:
import { library } from '@fortawesome/fontawesome-svg-core'
import { fab } from '@fortawesome/free-brands-svg-icons'
import { fas } from '@fortawesome/free-solid-svg-icons'
// This exports the whole icon packs for Brand and Solid.
library.add(fab, fas)
and to use them:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
<FontAwesomeIcon icon={['fab', 'apple']} /> // any icon from Brand.
<FontAwesomeIcon icon={['fas', 'coffee']} /> // any icon from Solid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With