How can I get Font Awesome 5 to work with React?

In the following example, the original icon is displayed, but it does not change when the class changes.

const Circle = ({ filled, onClick }) => {
  const className = filled ? 'fas fa-circle' : 'far fa-circle';
  
  return (
    <div onClick={onClick} >
      <i className={className} />
      <p>(class is {className})</p>
    </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} />;
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://use.fontawesome.com/releases/v5.0.2/js/all.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"></div>
Run codeHide result
+3
source share
3 answers

BO41 is right, this is a terrific javascript font that is not a render. If you are not using the new svg / javascript icons for fonts, you can use font-awesome as webfont with css.

In your index.html, remove the fontawesome script and add a stylish CSS stylesheet:

<link href="https://use.fontawesome.com/releases/v5.0.2/css/all.css" rel="stylesheet">

Your code should now work.


Another possibility is to use the official font fix pack (this is a bit more hassle, but it uses new svg icons)

Add the necessary packages for the project:

yarn add @fortawesome/fontawesome @fortawesome/react-fontawesome
yarn add @fortawesome/fontawesome-free-regular @fortawesome/fontawesome-free-solid

And updated 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} />;
  }
}

. github : https://github.com/FortAwesome/react-fontawesome

+5

fontawesome, svg <i>. .

https://fontawesome.com/how-to-use/svg-with-js

, , .

Awesome, .

i

<script>
  FontAwesomeConfig = { autoReplaceSvg: 'nest' }
</script>
<i class="my-icon fas fa-coffee" data-fa-mask="fas fa-circle"></i>

svg i

<i class="my-icon" data-fa-mask="fas fa-circle" data-fa-i2svg="">
  <svg class="svg-inline--fa fa-coffee fa-w-16" data-fa-mask="fas fa-circle" aria-hidden="true" data-fa-i2svg="" data-prefix="fas" data-icon="coffee" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">...</svg>
</i>
+1

, awesome javascript, . , ,

0

Source: https://habr.com/ru/post/1690417/


All Articles