【问题标题】:How can I put multiple conditions in a React component?如何在 React 组件中放置多个条件?
【发布时间】:2021-11-18 13:44:58
【问题描述】:

在 React 中,我可以有条件地渲染任何 div,例如:

{hasContent &&
   <span>{value}</span>
}

我正在尝试设置两个这样的条件:

{hasContent || hasDesc &&
   <span>{value}</span>
}

但它不起作用。

如果有hasContenthasDesc,我想渲染span

我该怎么做?

【问题讨论】:

    标签: javascript node.js reactjs next.js conditional-statements


    【解决方案1】:

    这只是运算符优先级:&amp;&amp; 优先于 ||。只需使用括号以您想要的方式处理它:

    {(hasContent || hasDesc) &&
       <span>{value}</span>
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-01
      • 2021-04-28
      • 2020-01-09
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 2017-11-21
      相关资源
      最近更新 更多