react-native - how to do conditional rendering and non-conditional rendering of both components in Map function react -native
问题描述
i want to display ItemName and if ItemDeliveryStatus==1 then show disabled TextInput else display editable TextInput inside Map function react-native
const [arrayList, setArraylist] = useState([
{ id: 0, ItemName: 'A', ItemDeliveryStatus:1 },
{ id: 1, , ItemName: 'B', ItemDeliveryStatus:1 },
{ id: 2, ItemName: 'C', ItemDeliveryStatus:1 },
{ id: 3, ItemName: 'D', ItemDeliveryStatus:1 },
]);
{
arrayList.map((items,index)=>{
return <View>
<Text >{items.ItemName}</Text>
if(items.ItemDeliveryStatus== 1){
return <View>
<TextInput editable={false} />
</View>
}else
{
<View>
<TextInput editable={true} />
</View>
}
</View>
解决方案
您可以使用花括号在 JSX 组件中嵌入表达式,并使用三元运算符作为条件,因此以下内容应该有效:
{ items.ItemDeliveryStatus == 1 ?
(<View>
<TextInput editable={false} />
</View>) :
(<View>
<TextInput editable={true} />
</View>)
}
它将在组件的返回内部工作。
例如:
...
return (
<View>
{
items.ItemDeliveryStatus == 1 ?
(<View>
<TextInput editable={false} />
</View>) :
(<View>
<TextInput editable={true} />
</View>)
}
</View>
)
在这里,给你做了个小零食,代码完整。
推荐阅读
- react-native - TypeError:null 不是对象(评估'_reactNativeDefaultPreference.default.get')
- postgresql - Postgresql Has-Many 关系插入相同的 id 错误
- jasper-reports - Jasper Sub 报告尺寸问题
- linux - 如何删除 SWupdate 安装中的 gpiod 错误
- java - java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.util.Iterator java.util.ArrayList.iterator()”
- java - 您将如何为构造函数创建测试?
- python - 如何在不使用 for 循环的情况下仅更新二维列表中的选定值?
- python - 保持具有相同结果的行或应该有一些熊猫的行
- javascript - 如何使用firebase auth在nodejs的服务器端验证用户电话号码
- android - 如何使用 Kotlin 从 EditText 获取 LiveData 值并在 TextView 中显示?MVVM