2017-07-05 37 views
0

我期待從遠程服務器的一些數據顯示在屏幕上,但我得到了這個錯誤在模擬器Cannot read property 'undefined' of undefined之所以內容是之前呈現的原因結果的到來,這個代碼的部分在那裏我有這個錯誤:反應本機如何處理無法讀取屬性undefined

caption = { this.state.customFieldDropdown['gender'][this.state.dropDownSelectedItems['gender'] ] || '---Choose---' } 

所以物業caption期待一個文本,並處理未定義的值添加此|| '---Choose---'顯示文本'---Choose---'在情況爲空,但問題是this.state.dropDownSelectedItems['gender']未定義,當this.state.customFieldDropdown訪問該值時,它導致錯誤(讀取屬性這是未定義的)

那麼如何處理這個問題呢?

回答

0

Idx真的很好。你可以這樣做:

if (idx(this.state, _ => _.customFieldDropdown.gender[_.dropDownSelectedItems.gender])) 
    caption = this.state.customFieldDropdown.gender[this.state.dropDownSelectedItems.gender] 
else 
    caption = '---Choose---' 
相關問題