Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 为什么React Native返回与布尔值等效的字符串?_React Native - Fatal编程技术网

React native 为什么React Native返回与布尔值等效的字符串?

React native 为什么React Native返回与布尔值等效的字符串?,react-native,React Native,我正在计算一些变量,并使用结果有条件地呈现元素 这是评估 const condition = this.props.badgeCount > 0 用这样的结果 <View> {condition && ( <View> <Text> {this.props.badgeCount} </Text> </View> )} </View> 因

我正在计算一些变量,并使用结果有条件地呈现元素

这是评估

const condition = this.props.badgeCount > 0
用这样的结果

<View>
  {condition && (
    <View>
      <Text>
        {this.props.badgeCount}
      </Text>
    </View>
  )}
</View>
因此,返回的结果显然类似于
“true”
“false”


我的问题是为什么会这样,因为这显然是一个应该返回布尔值的求值,而且除了我正在使用的方法之外,还有其他方法可以解决这个问题吗?

这是因为你在那里做的是一个内联if语句。任何不是空字符串、
null
未定义的字符串都是真实值()

您可以通过进入浏览器的控制台并执行以下操作来轻松测试:

console.log('someString' ? true : false);
console.log('' ? true : false);
您可以通过执行
来解决您的问题!!将字符串值转换为布尔值从而不会触发
文本字符串的条件必须在文本组件中呈现
错误:

<View>
    {!!condition && (
        <View>
        <Text>
            {this.props.badgeCount}
        </Text>
        </View>
    )}
</View>

{!!条件&&(
{this.props.badgeCount}
)}

有关
的更多信息(不是)在这个答案中:

这是因为您在那里做的是一个内联if语句。任何不是空字符串、
null
未定义的字符串都是真实值()

您可以通过进入浏览器的控制台并执行以下操作来轻松测试:

console.log('someString' ? true : false);
console.log('' ? true : false);
您可以通过执行
来解决您的问题!!将字符串值转换为布尔值从而不会触发
文本字符串的条件必须在文本组件中呈现
错误:

<View>
    {!!condition && (
        <View>
        <Text>
            {this.props.badgeCount}
        </Text>
        </View>
    )}
</View>

{!!条件&&(
{this.props.badgeCount}
)}
有关
的更多信息(不是)在这个答案中:

N.B.
Boolean(foo)
具有相同的效果!!foo
.N.B<代码>布尔值(foo)
具有相同的效果!!foo