Font-awesome: Icon not rerendering in react

Created on 20 Dec 2017  路  8Comments  路  Source: FortAwesome/Font-Awesome

Here's an example: https://codesandbox.io/s/m55100zjqy

Using the CDN. If you click an item in the table, the icon will not change however the colors will. The code to take note of is

const iconClass = data.selected ? "fas fa-check-square" : "far fa-square"

Most helpful comment

If you want a quick and dirty work around for this you could have something along the following:

render(){
    const icon = someCondition ? "fa fa-info-circle" : "fa fa-bolt"
    return (
        <i key={icon}>
             <span className={icon} />
        </i>
    )
}

A change in the key prop will rerender the dom element, also triggering fontawesome to rerender

All 8 comments

Duplicate of #11950.

Hi!

Thanks for being part of the Font Awesome Community.

I'm going to consider this a duplicate of #11950.

If the fix for #11950 will not solve this, please comment here and I will reopen

Yeah, this isn't the same thing unfortunately. It changes color fine, but it should be changing the icon as well.

We worked out a fix by importing the CSS instead of relying on the CDN. But this is something that should probably be mentioned somewhere else!

Edit:
Started using the SVG with JS CDN and it didn't work. Switched to the Web Fonts with CSS and it worked!

I'm seeing exactly the same thing as @blaynem-moovel . This isn't related to the issue this has been closed a duplicate of.

Looking at the code @blaynem-moovel wrote, I'm doing something similar. His has

      const iconClass = data.selected ? "fas fa-check-square" : "far fa-square"
      return (
        <tr className={rowClass} key={data.ID} onClick={() => this.handleClick(data.ID)}>
          <th scope="row"><i className={iconClass} /></th>

and mine has

  <div className="flyout-control" onClick={onMenuClick}>
     <i className={`fas ${isOpen ? "fa-times" : "fa-bars"} fa-2x fa-fw`} aria-hidden="true"></i>
  </div>

The key here is that when React re-renders the <i> component, only the className may have changed.

My same code was working fine with Font Awesome 4, and this broke trying to move to 5. I'm using the JS-only approach. Moving to CSS appears to have fixed it for @blaynem-moovel , but I'd rather stay with JS as I moved for the tree-shaking.

If you are using React make sure and use https://www.npmjs.com/package/@fortawesome/react-fontawesome instead or switch back to the web fonts with CSS version of FA5.

@robmadole ty sir.

I had this same issue. I found this helpful: https://stackoverflow.com/a/49667476/1807627

If you want a quick and dirty work around for this you could have something along the following:

render(){
    const icon = someCondition ? "fa fa-info-circle" : "fa fa-bolt"
    return (
        <i key={icon}>
             <span className={icon} />
        </i>
    )
}

A change in the key prop will rerender the dom element, also triggering fontawesome to rerender

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Vivalldi picture Vivalldi  路  200Comments

QuincyLarson picture QuincyLarson  路  308Comments

jrf0110 picture jrf0110  路  170Comments

vivekagr picture vivekagr  路  194Comments

bhubbard picture bhubbard  路  169Comments