Reputation: 3892
I am facing issue in react.js with font-awesome icon. Here I would like to render different icons using conditional operator. I am able to see the value of span tag rendering correctly but not the font awesome icon.
I am able to see icons are rendered as svg here. But I am not sure what is causing this issue.
Any help is appreciated.
icon links solid: https://fontawesome.com/icons/star?style=solid
icon links regular: https://fontawesome.com/icons/star?style=regular
jsFiddle: https://jsfiddle.net/69z2wepo/182809/
class Hello extends React.Component {
constructor(props){
super(props);
this.state = {
toggle: true
}
}
toggle(){
this.setState({toggle: !this.state.toggle});
}
render() {
return (
<div>
Hello {this.props.name}
{
this.state.toggle ? (
<span><i className="fas fa-star"></i>solid</span>
) : (
<span><i className="far fa-star default-preference"></i>regular</span>
)
}
<div>
<button onClick={this.toggle.bind(this)}> Toggle </button>
</div>
</div>
)
}
}
ReactDOM.render(
<Hello name="World" />,
document.getElementById('container')
);
Upvotes: 2
Views: 4275
Reputation: 9338
The font-awesome javascript doesn't rerender on a React rerender trigger. If you are okay with not using the new font-awesome svg/javascript icons, you can use font-awesome as a webfont with css.
In your index.html, delete the fontawesome script, and add the font-awesome css stylesheet:
<link href="https://use.fontawesome.com/releases/v5.0.2/css/all.css" rel="stylesheet">
Your code should work now.
The other possibility is to use the official font-awesome react package
Add necessary packages to project:
yarn add @fortawesome/fontawesome @fortawesome/react-fontawesome
yarn add @fortawesome/fontawesome-free-regular @fortawesome/fontawesome-free-solid
Example code:
import fontawesome from '@fortawesome/fontawesome'
import FontAwesomeIcon from '@fortawesome/react-fontawesome'
import { faCircle as fasCircle } from '@fortawesome/fontawesome-free-solid'
import { faCircle as farCircle } from '@fortawesome/fontawesome-free-regular'
const Circle = ({ filled, onClick }) => {
return (
<div onClick={onClick} >
<FontAwesomeIcon icon={filled ? farCircle : fasCircle}/>
</div>
);
};
class App extends React.Component {
state = { filled: false };
handleClick = () => {
this.setState({ filled: !this.state.filled });
};
render() {
return <Circle filled={this.state.filled} onClick={this.handleClick} />;
}
}
See the github repo for more information: https://github.com/FortAwesome/react-fontawesome
Upvotes: 4
Reputation: 470
You can also use React Icons https://gorangajic.github.io/react-icons/
React Icons currently Supported following icons
Material Design Icons by Google https://www.google.com/design/icons/ (licence: CC-BY 4.0)
Font Awesome by Dave Gandy - http://fontawesome.io (licence: SIL OFL 1.1)
Typicons by Stephen Hutchings - http://typicons.com (licence: CC BY-SA)
Github Octicons icons by Github https://octicons.github.com/ (licence: SIL OFL 1.1)
etc..
Upvotes: 0
Reputation: 808
I added this (get it from here https://fontawesome.com/get-started ) and it worked fine
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg" crossorigin="anonymous">
and deleted the fontawesome js file
Upvotes: 1