2016-05-16 32 views
0

我有一個數組如果在條件檢驗發生反應JSX爲空值[ES6]

const myArray = this.props.values.students; 

如何顯示無如果數組爲空?

這是我目前使用...

<p>{this.props.values.students ? myArray : 'None' }</p> 

它似乎並沒有呈現「無」,如果數組中,其實是空的。我該如何做這項工作?

+2

檢查數組的長度? '{this.props.values.students.length? ...:'無'}' – andlrc

回答

9

的問題是,一個空數組不是falsy值:

if ([]) { 
    console.log('truly - this will happen'); 
} 
else { 
    console.log('false - this will *never* happen'); 
} 

可以然而檢查數組的長度,這將給出falsy值時空(0)

<p>{this.props.values.students.length ? myArray : 'None' }</p> 
+1

非常明確的解釋!你的意思是「truthy」而不是「真正的」?另外,爲了進一步的清晰/簡潔,可能值得使用'myArray.length'。 –

+0

完美謝謝:D – Modelesq