【问题标题】:Using an if/else inside react render return在反应渲染返回中使用 if/else
【发布时间】:2016-11-10 14:32:18
【问题描述】:

我的反应渲染中有这个 javascript:

  <p id='canPlay'>
          {this.state.canPlayArray.map(function(num, index){
            return <p key={ index }>Name: {num.name} Date Indicated: {num.dateConfirmed}</p>;
          }, this)}
   </p>

但有时,canPlayArray 可能为空。我怎么能说如果 array.length >0 然后渲染数组 ELSE 显示'没有人响应'

我不能在我认为的 html 中执行 if/else 并且在 return() 之前我有:

if (this.state.info){
      output = <Thanks />;
    }

这可能会阻止我在那里执行 if/else?

考虑使用三元运算符,但不知道放在哪里

【问题讨论】:

标签: javascript html reactjs


【解决方案1】:
<p id='canPlay'>
  {
    this.state.canPlayArray.length
    ? this.state.canPlayArray.map(function(num, index){
      return <p key={ index }>Name: {num.name} Date Indicated: {num.dateConfirmed}</p>;
    }, this)
    : <p>no one has responded</p>
  }
</p>

【讨论】:

猜你喜欢
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
相关资源
最近更新 更多