如何在React中检查对象是否为null,空或未定义?

时间:2018-08-17 12:15:11

标签: javascript reactjs

我在<a href='/' class='non-underline-link'> <p>Hello!</p> </a> 中有以下代码序列:

React

我还必须检查{financialPerformance && ( financialPerformance.isConfirmed ? ( <L.Text txtGray>Confirmed</L.Text> ) : ( <L.Text txtGray>Not Confirmed</L.Text> ) )} 本身是否为financialPerformance或为空或null并显示“未确认” 消息。我的意思是第一次出现undefined对象。

financialPerformance

我该怎么做?

1 个答案:

答案 0 :(得分:5)

由于nullundefined在布尔上下文中将被评估为false-您只需要在一个地方合并支票即可:

{
  financialPerformance && financialPerformance.isConfirmed ? (
    <L.Text txtGray>Confirmed</L.Text>
  ) : (
    <L.Text txtGray>Not Confirmed</L.Text>
  )
}
相关问题