javascript - React.js not rendering correct font awesome icon - Stack Overflow

I am facing issue in react.js with font-awesome icon. Here I would like to render different icons using

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:

icon links regular:

jsFiddle: /

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')
);

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./icons/star?style=solid

icon links regular: https://fontawesome./icons/star?style=regular

jsFiddle: https://jsfiddle/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')
);
Share Improve this question asked Apr 27, 2018 at 11:15 Gopinath ShivaGopinath Shiva 3,8905 gold badges28 silver badges50 bronze badges 2
  • The problem is that you're using SVG inline from JS file instead you should use icon font (css file). – jcubic Commented Apr 27, 2018 at 11:46
  • Use CSS from cdn jsfiddle/69z2wepo/182815 – It worked yesterday. Commented Apr 27, 2018 at 12:18
Add a ment  | 

3 Answers 3

Reset to default 4

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./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./FortAwesome/react-fontawesome

I added this (get it from here https://fontawesome./get-started ) and it worked fine

<link rel="stylesheet" href="https://use.fontawesome./releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg" crossorigin="anonymous">

and deleted the fontawesome js file

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./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. (licence: CC BY-SA)
Github Octicons icons by Github https://octicons.github./ (licence: SIL OFL 1.1)

etc..

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745288631a4620719.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信