您好,我被困在這個問題上,我無法弄清楚如何在反應本機中進行行內 if else 陳述句。
{currentSlideIndex == 0 ? (
<>
<KeyboardAvoidingView style={{flex:1}} behavior='position'>
<BlurView intensity={20} tint="light" >
<Text style={styles.label}>Name</Text>
<TextInput
value={name}
onChangeText={onNameChange}
style={styles.input}
/>
</BlurView>
</KeyboardAvoidingView>
</>
):(
<>
<KeyboardAvoidingView style={{flex:1}} behavior='position'>
<BlurView intensity={20} tint="light" >
<Text style={styles.label}>Surname</Text>
<TextInput
value={name}
onChangeText={onNameChange}
style={styles.input}
/>
</BlurView>
</KeyboardAvoidingView>
</>
)}
我希望它做到這一點,以便我有 currentslideIndex == 0 , currentslideIndex == 1 ,currentslideIndex == 2。
uj5u.com熱心網友回復:
試試下面的代碼:
{
currentSlideIndex == 0 ? (
<>
{/* code for slide 0 */}
</>
) : (currentSlideIndex == 1 ? (
<>
{/* code for slide 1 */}
</>
) : (
<>
{/* code for slide 2 */}
</>
)
)
}
uj5u.com熱心網友回復:
if-else 陳述句在 JSX 中不起作用。這是因為 JSX 只是函式呼叫和物件構造的語法糖。
所以在 Jsx 中它實際上變成了類似的東西
render(if (true) return 0)
// This doesn't work because you can't put conditional inside function calls
但是,如果您在 jsx 中進行映射,則可以使用{ x.map(x => { if (true) return <Component //>)}。對于條件,您只能使用三元。
但是您可以通過使用立即呼叫函式運算式 (IIFE) 來實作您所說的
render() {
return (
<View style={styles.container}>
{(() => {
if (this.state == 'news'){
return (
<Text>data</Text>
)
}
return null;
})()}
</View>
)
}
uj5u.com熱心網友回復:
你想要類似的東西:
{currentSlideIndex === 0 ?
(...) :
currentSlideIndex === 1 ?
(...) : //currentSlideIndex === 2
(...)
}
您實際上可以嵌套三元組,以便有多個 if/else if 條件!
或者:
你可以在你的渲染函式回傳之前有一個變數,即
let keyboardAvoidingView;
if (currentSlideIndex === 0) {
keyboardAvoidingView = ...
}
else if (currentSlideIndex === 1) {
keyboardAvoidingView = ...
}
else {
keyboardAvoidingView = ...
}
然后在渲染中,只渲染你的變數:
{keyboardAvoidingView}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/478687.html
標籤:javascript 反应式
