简体   繁体   English

字体真棒 5:条件变化时图标不切换

[英]Font awesome 5: icon not switching when condition changes

I'm using font awesome 5 in a react.js project, and having trouble make the icon switch in a conditional statement.我在 react.js 项目中使用 font awesome 5,并且无法在条件语句中进行图标切换。 Here's the sample code:这是示例代码:

!isRecording
&&
<div style={styles.recordButton} onClick={e => this.record(e)}>
    <span className="fa-layers  fa-4x">
      <div><i className="fas fa-circle " style={{color: Colors.mainOrange}}></i></div>
      <div><i className="fa fa-microphone  fa-inverse" data-fa-transform="shrink-6"></i></div>
    </span>
</div>
||
<div style={styles.recordButton} onClick={e => this.stop(e)}>
    <span className="fa-layers  fa-4x">
      <div><i className="fas fa-circle " style={{color: Colors.mainOrange}}></i></div>
      <div><i className="fas fa-stop  fa-inverse" data-fa-transform="shrink-6"></i></div>
    </span>
</div>

When isRecording == true , the icon should change from fa-microphone to fa-stop .isRecording == true时,图标应该从fa-microphone变为fa-stop But the switch doesn't happen.但是切换不会发生。 The microphone icon stays.麦克风图标保持不变。

Strangely, when I don't use the fa-layers class in the button to be switched in, the switch happens.奇怪的是,当我不使用要切换的按钮中的fa-layers class 时,就会发生切换。 But the transition is super awkward--icon size and position shift:但是过渡非常尴尬——图标大小和 position 偏移:

!isRecording
&&
<div style={styles.recordButton} onClick={e => this.record(e)}>
    <span className="fa-layers  fa-4x">
      <div><i className="fas fa-circle " style={{color: Colors.mainOrange}}></i></div>
      <div><i className={isRecording ? `fa fa-stop  fa-inverse` : `fa fa-microphone  fa-inverse`} data-fa-transform="shrink-6"></i></div>
    </span>
</div>
||
<div style={styles.recordButton} onClick={e => this.stop(e)}>
    <span className=" fa-2x">
      <div key="stopRecord"><i className="fa fa-stop-circle fa-2x" style={{color: Colors.mainOrange}}></i></div>
    </span>
</div>

Any idea why this is happening?知道为什么会这样吗?

I've experimented around with your code and realized that you are using FontAwesome's new SVG JavaScript library which completely explains the problem. 我已经尝试了你的代码,并意识到你正在使用FontAwesome的新SVG JavaScript库,它完全解释了这个问题。 The code that that library uses to look at the DOM and insert the icons doesn't play well with React. 该库用于查看DOM并插入图标的代码与React不兼容。 This is also discussed here . 这也在这里讨论。

To solve this problem, you can either ... 要解决这个问题,你可以......

Don't duplicate code for little changement . 不要重复代码以进行少量更改

If you just have to change a class/content use an expression in your className 如果您只需更改类/内容,请在className使用表达式

And change your called method in onClick to check the state of isRecording to call the right method ( stop/record ) 并在onClick更改被调用的方法以检查isRecording的状态以调用正确的方法( stop/record

 handleMicroClick(e){ this.state.isRecording ? this.stop(e) : this.record(e); } 
 <div style={styles.recordButton} onClick={e => handleMicroClick(e)}> <span className="fa-layers fa-4x"> <div> <i className="fas fa-circle " style={{color: Colors.mainOrange}}> </i> </div> <div> <i className={isRecording ? 'fa-microphone' : 'fa-stop'} data-fa-transform="shrink-6"> </i> </div> </span> </div> 

Old question but I also have the same issue and solved without using the proposed alternatives.老问题,但我也有同样的问题,并且在不使用建议的替代方案的情况下解决了。

You can make use of key property, forcing React to update your component你可以使用key属性,强制 React 更新你的组件

!isRecording
&&
<div key={Math.random()} style={styles.recordButton} onClick={e => this.record(e)}>
    <span className="fa-layers  fa-4x">
      <div><i className="fas fa-circle " style={{color: Colors.mainOrange}}></i></div>
      <div><i className={isRecording ? `fa fa-stop  fa-inverse` : `fa fa-microphone  fa-inverse`} data-fa-transform="shrink-6"></i></div>
    </span>
</div>
||
<div key={Math.random()} style={styles.recordButton} onClick={e => this.stop(e)}>
    <span className=" fa-2x">
      <div key="stopRecord"><i className="fa fa-stop-circle fa-2x" style={{color: Colors.mainOrange}}></i></div>
    </span>
</div>

Then it works as expected;)然后它按预期工作;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM