誰能告訴我,如果使用另一種使用..其它..內部條件的反應對象如何,如果其他條件中反應物
const background = {
color: {
if (this.props.status == 'broker') {
background : '#FFFFFF'
} esle {
background : 'green'
}
}
}
誰能告訴我,如果使用另一種使用..其它..內部條件的反應對象如何,如果其他條件中反應物
const background = {
color: {
if (this.props.status == 'broker') {
background : '#FFFFFF'
} esle {
background : 'green'
}
}
}
您不需要全部內聯評估它。嵌套的三元語句會變得非常難看。您可以創建一個函數並將結果傳遞給:
function bgColor(status) {
if (status === 'broker') {
return { background: '#fff' }
}
if (status === 'whatever' {
// return ...
}
// whatever else
}
const background = {
color: bgColor(this.props.status)
}
你可以使用一個三元:
const background = {
color: this.props.status === 'broker' ? '#ffffff' : 'green'
}
如果您需要更多條件,這可能會受到限制和/或難以閱讀,因此您也可以使其發揮作用,但是您只需要小心如何/在哪裏調用它:
const background = {
color: function() {
let color
if(this.props.status === 'broker') {
color = '#ffffff'
} else {
color = 'green'
}
return color
}
}
如果我想使用兩個以上的條件呢? –
@radjeep嵌套ternarys? –
@RajdeepRatan我編輯了我的答案。你可以使用Jonas w建議的嵌套三元組,但如果這些三元組難以閱讀,可以使你的屬性成爲一個函數。 –
您也可以使用二元運算符:'-5 || ''// -5' – fungusanthrax