简体   繁体   English

尝试根据当前 state 有条件地渲染 react-fontawesome 图标

[英]Trying to conditional render react-fontawesome icon based on current state

I have a browser based game I am creating with React.我有一个使用 React 创建的基于浏览器的游戏。 The user will have lives that will decrease when a given event occurs.当给定事件发生时,用户的生命将减少。 Lives are displayed to the player via 3 font-awesome heart icons.生命通过 3 个字体真棒的心形图标显示给玩家。 I am using react-fontawesome.我正在使用 react-fontawesome。

Getting the icons to display is not an issue.让图标显示不是问题。 I am looking for a cleaner way to display the 3 heart icons based off state (trying not to use nested if statements or switch statement if possible).我正在寻找一种更简洁的方式来显示基于 state 的 3 个心形图标(尽可能不使用嵌套的 if 语句或 switch 语句)。

// initial state

state = { lives: 3 }



// import of FA icon with react-fontawesome

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'

import { faHeart } from '@fortawesome/free-solid-svg-icons'



// assigning icon to variable

let hearts = <FontAwesomeIcon icon={faHeart} className="mx-1" />;



// displaying icon

<div className="d-flex panel-content-md">{ hearts }</div>

When the state of 'lives' decreases, a heart icon should be removed indicating to the player they have loss a life.当“生命”的 state 减少时,应移除一个心形图标,向玩家表明他们失去了一条生命。 I have found to display multiple icons in the 'hearts' variable, you have to wrap the tags in a parent div tag:我发现在“hearts”变量中显示多个图标,您必须将标签包装在父 div 标签中:

let hearts = (
    <div>
        <FontAwesomeIcon icon={faHeart} className="mx-1" />
        <FontAwesomeIcon icon={faHeart} className="mx-1" />
        <FontAwesomeIcon icon={faHeart} className="mx-1" />
    </div>
)

I have not found a clean conditional rendering solution that just looks at the number that is assigned to 'lives' and updates the amount of hearts that are displayed.我还没有找到一个干净的条件渲染解决方案,它只查看分配给“生命”的数字并更新显示的心的数量。 I have a feeling I am overlooking the obvious here.我有一种感觉,我在这里忽略了显而易见的事情。

You could to this by mapping an array of length equal to your state.lives value:您可以通过映射长度等于您的 state.lives 值的数组来实现这一点:

<div>
  {
     [...Array(this.state.lives).keys()].map(i => (
        <FontAwesomeIcon key={i} icon={faHeart} className="mx-1" />
     )
  }
</div>

Or just store your lives as an array or as a string of one character per life或者只是将你的生命存储为一个数组或每个生命一个字符的字符串

// this.state.livesArray is [1,2,3]
<div>
  {
     this.state.livesArray.map(i => (
        <FontAwesomeIcon key={i} icon={faHeart} className="mx-1" />
     )
  }
</div>
// this.state.livesString is "❤❤❤"
<div>
  {
     this.state.livesString.split('').map(i => (
        <FontAwesomeIcon key={i} icon={faHeart} className="mx-1" />
     )
  }
</div>

Or simply by conditional rendering或者简单地通过条件渲染

let heart = <FontAwesomeIcon icon={faHeart} className="mx-1" />;


<div className="d-flex panel-content-md">
  {this.state.lives >= 1 && heart }
  {this.state.lives >= 2 && heart }
  {this.state.lives >= 3 && heart } // or === 3 if you don't want to add extra lives in the future
</div>

A bit simpler Code,更简单的代码,

render() {
  let hearts = [];

  for(let i=0; i<this.state.lives; i++) {
    hearts.push(<FontAwesomeIcon key={i} icon={faHeart} className="mx-1" />)
  }

  return(
    <div>{hearts}</div>
  );
}

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

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