简体   繁体   English

隐藏Font Awesome图标组件,并根据状态显示另一个

[英]Hide Font Awesome icon component and display another based on state

I'm building a pomodoro timer with 4 sessions. 我正在建立一个具有4个会话的番茄定时器。 When a 25-minute timer is up, 1 is added to a state variable sessionNumber . 当25分钟计时器计时到时, sessionNumber 1加到状态变量sessionNumber I have four of these circle/unchecked checkboxes displayed when the pomodoro cycle starts: 番茄周期开始时,我有四个显示的圆形/未选中的复选框:

<FontAwesomeIcon
  icon={faCircle}
  size="2x"
  className="pomodoro-unchecked session-checkbox"
/>

Each time 1 is added to sessionNumber state, I would like to hide one and display a different icon component: 每次将1添加到sessionNumber状态时,我想隐藏一个并显示不同的图标组件:

<FontAwesomeIcon
  icon={faCheckCircle}
  size="2x"
  className="pomodoro-checked session-checkbox"
/>

The only ways I could think of doing this would take a whole lot of code -- for example if statements based on each session number, like with an if statement, if the session is 0, display 4 unchecked circles (with the code for all four components), and if the session is 1, display 1 checked circle and 3 unchecked circles, and so forth. 我想到的唯一方法是花费大量代码-例如,基于每个会话号的if语句(如if语句,如果会话为0,则显示4个未选中的圆圈(所有代码均包含) (四个组成部分),并且如果会话为1,则显示1个选中的圆圈和3个未选中的圆圈,依此类推。 The second way I considered would be to give each one a different class name and in the method that changes the session number, display and hide each one, based on which session number it is (that would be more complicated). 我考虑的第二种方法是给每个人一个不同的类名,并在更改会话号的方法中,根据它是哪个会话号来显示和隐藏每个人(那会更复杂)。 Is there a simpler, more succinct method? 有没有更简单,更简洁的方法?

You could use an array in state . 您可以在state使用数组。 You could initialise the array with four "faCircle" and then use the array.fill() method on setState to fill up the icons with "faCheckCircle". 您可以使用四个“ faCircle”初始化数组,然后在setState上使用array.fill()方法使用“ faCheckCircle”填充图标。 Then you can map over the array to render the appropriate icon. 然后,您可以映射到数组以呈现适当的图标。

class SessionIcons extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      sessions: 0,
      icons: ["faCircle", "faCircle", "faCircle", "faCircle"]
    };
  }

  onAddSession = () => {
    this.setState(state => ({
      sessions: state.sessions + 1,
      icons: state.icons.fill("faCheckCircle", 0, state.sessions + 1)
    }));
  };

  render() {
    return (
      <div>
        {this.state.icons.map((icon, index) => (
          <FontAwesomeIcon
            key={icon + index}
            icon={icon === "faCircle" ? faCircle : faCheckCircle}
            size="2x"
          />
        ))}
        <button onClick={this.onAddSession}>Add session</button>
      </div>
    );
  }
}

Demo: https://codesandbox.io/s/shy-tdd-qzs1n 演示: https//codesandbox.io/s/shy-tdd-qzs1n

 class Timer extends React.Component{
  state = {  sessionNumber: 1}

 checkTimer = () =>{
..your logic to check timer every 25 mins
 this.setState({timerState:1})
  }
render(){
Let font;
if( this.state.sessionNumber == 1)
 {
    font = <FontAwesomeIcon
    icon={faCircle}
   size="2x"
   className="pomodoro-unchecked session-checkbox"
  />
 }
   else if(this.state.sessionNumber == 2)
   {      
    font = FontAwesomeIcon
  icon={faCheckCircle}
  size="2x"
  className="pomodoro-checked session-checkbox"
    />
   return(

        {font}


     )
    }
   }

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

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