如何在 React Native 中用两个语句编写 if 条件 [英] How to write if condition with two statement in React Native

查看:63
本文介绍了如何在 React Native 中用两个语句编写 if 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是很困惑如何在 React Native

例如,

if(array.length != null && array.length >= 2){
    alert("Array Is Greater Than 2")
}else if(array.length != null ){
    alert("Array Is Less Than 2")
}else{
    alert("Array is null")
}

我知道如何在 React Native 中传递信号条件,例如

I know how to pass signle condition in React Native like

例如,

{
    array.length != null &&
    <View>
        /* Content Here */
    </View>
}

{
    array.length != null ?
    <View>
        /* If Content Here */
    </View>
    :
    <View>
        /* Else Content Here */
    </View>
}

但是如果状态像第一个一样,我该如何创建?

But How do I create if state like first one ?

推荐答案

你可以有嵌套的三元条件检查

You can have nested ternary condition check like

{
    array.length != null && array.length >= 2 ?
    <View>
        /* If Content Here */
    </View>
    :
    array.length != null?
     <View>
        /* Else if Content Here */
    </View>: 
    <View>
        /* Else Content Here */
    </View>
}

但是,您可以按照@MayankShukla 的建议,在从渲染调用的函数中使用 if-else 简单地执行此操作,因为它比嵌套三元运算符更具可读性

However you can simply do it using if-else in a function which you call from render as suggested by @MayankShukla since it is more readable then nested ternary operators

这篇关于如何在 React Native 中用两个语句编写 if 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆