繁体   English   中英

在 jsx 和 map 中响应 if 语句

[英]react if statement in jsx and map

我有工作代码,并且 map 函数中的 if 语句没有什么问题,这里是代码

    const SortableItem = SortableElement(CashItem);
const SortableList = SortableContainer(({items}) => {
  return (
    <div>
      {items.map((cashitem, index) => (
        <SortableItem key={`item-${index}`} 
          index={index} 
          isPayed={cashitem.isPayed}
          date={cashitem.date}
          name={cashitem.name}
          realisticVal={cashitem.realisticVal}
          realisticBalance={cashitem.realisticBalance}
          optimisticBalance={cashitem.optimisticBalance}
          optimisticVal={cashitem.optimisticVal}
          changedName={(event) => this.nameChangedHandler(event, cashitem.id)} 
          isBlocked={(event) => this.handleChangeCheckbox(event, cashitem.id)} 
          delete={(event) => this.removeItem(event, cashitem.id)} 
          addNext={(event) => this.addNext(event, cashitem)} 
          realisticChange={(event) => this.realisticBalanceChangedHandler(event, cashitem.id)}  
          optimisticChange={(event) => this.optimisticBalanceChangedHandler(event, cashitem.id)}  
          dateChangedHandler={(event) => this.dateChangedHandler(event, cashitem.id)}
         />
      ))}
    </div>
  );
});

现在我想检查 if 语句仅在地图中的 cashitem 有状态可见时才呈现 cashitems isVisible 已经有 isVisible:true 或 false 我想做类似的事情

 const SortableItem = SortableElement(CashItem);
const SortableList = SortableContainer(({items}) => {
  return (
    <div>
      {items.map((cashitem, index) => (
        if(cashitem.isVisible===true){
          <SortableItem key={`item-${index}`} 
          index={index} 
          isPayed={cashitem.isPayed}
          date={cashitem.date}
          name={cashitem.name}
          realisticVal={cashitem.realisticVal}
          realisticBalance={cashitem.realisticBalance}
          optimisticBalance={cashitem.optimisticBalance}
          optimisticVal={cashitem.optimisticVal}
          changedName={(event) => this.nameChangedHandler(event, cashitem.id)} 
          isBlocked={(event) => this.handleChangeCheckbox(event, cashitem.id)} 
          delete={(event) => this.removeItem(event, cashitem.id)} 
          addNext={(event) => this.addNext(event, cashitem)} 
          realisticChange={(event) => this.realisticBalanceChangedHandler(event, cashitem.id)}  
          optimisticChange={(event) => this.optimisticBalanceChangedHandler(event, cashitem.id)}  
          dateChangedHandler={(event) => this.dateChangedHandler(event, cashitem.id)}
         />
        }

      ))}
    </div>
  );
});
{
     items.map((cashitem, index) => { //<== change it to curly braces

        return cashitem.isVisible && (<SortableItem key={`item-${index}`} //<== using the return keyword
          ... 
        />)
        
      }
    }

不要使用括号,而是将其更改为花括号,并在 if else 条件中使用return关键字,如上所述

您没有在 if 语句中返回组件。

{items.map((cashitem, index) => {
    if(cashitem.isVisible){
        return <SortableItem key={`item-${index}`} ...otherProps/>
    }
})}

但是,既然您正在过滤列表,为什么不使用Array.filter方法呢?

{items.filter(cashItem => cashItem.isVisible).map((cashitem, index) => (
    <SortableItem key={`item-${index}`} ...otherProps/>
)}

由多个表达式组成的箭头函数需要:

  • 围绕它的大括号
  • 显式返回语句

因此:

(cashitem, index) => {
    if (condition) {
        return <Component />
    }
}

创建一个处理 if/else 语句并返回 jsx/html 构建器的函数,然后在 jsx render() 函数中调用它。

检查此解决方案/作者: Blair Anderson

renderSkillSection: function(){
  if (this.state.challengeChoices.length < 0) {                               
    return this.state.challengeChoices.map((para2, i) =>
         <ChallengeSkill key={i} {...para2} callback={this.madeSelection} />)
  }
  else {
    return <div>Hello world</div>
  }   
},

render: function(){
 return (<div className="skillSection">
  {this.renderSkillSection()}   
 </div>)
}

暂无
暂无

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

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