简体   繁体   English

React Native - 挂钩 useState 空数组

[英]React Native - hooks useState empty array

Consider the following piece of code in React Native:考虑 React Native 中的以下代码:

import React, { useState } from 'react';
import { Text, View } from 'react-native';

export default function App() {
  const [recommendations, setRecommendation] = useState<any[]>([]);
  return (
    <View>
      {recommendations != [] ? 
      (
        <Text>A</Text>
      ) : (
        <Text>B</Text>
      )}
    </View>
  );
}

Currently this always returns A and never enters B. Why?目前这总是返回 A 而从不进入 B。为什么?

in short, because the condition recommendations != [] always evaluates to true.简而言之,因为条件recommendations != []总是评估为真。 If you want to compare arrays, you'll need to do a deep comparison.如果要比较 arrays,则需要进行深入比较。 This article may be helpful: How to compare arrays in JavaScript?这篇文章可能会有所帮助: 如何比较 JavaScript 中的 arrays?

The expression you are using always returns true .您使用的表达式始终返回true That's why you get A as output.这就是为什么你得到 A 为 output。

If you want to return B when there are no elements in the array and A when there are, you can use the length of the array to achieve this.如果要在数组中没有元素时返回B,而在有元素时返回A,可以使用数组的长度来实现。

import React, { useState } from 'react';
import { Text, View } from 'react-native';

export default function App() {
  const [recommendations, setRecommendation] = useState<any[]>([]);
  return (
    <View>
      {recommendations.length ? 
      (
        <Text>A</Text>
      ) : (
        <Text>B</Text>
      )}
    </View>
  );
}

The MDN documentation says the following about comparison operators : MDN 文档对比较运算符进行了以下说明:

Features of comparisons:比较的特点:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.当两个字符串具有相同的字符序列、相同的长度和对应位置的相同字符时,它们是严格相等的。
  • Two numbers are strictly equal when they are numerically equal (have the same number value).当两个数字在数值上相等(具有相同的数值)时,它们是严格相等的。 NaN is not equal to anything, including NaN. NaN不等于任何东西,包括 NaN。 Positive and negative zeros are equal to one another.正零和负零彼此相等。
  • Two Boolean operands are strictly equal if both are true or both are false .如果两个 Boolean 操作数都为true或都为false ,则它们是严格相等的。
  • Two distinct objects are never equal for either strict or abstract comparisons.对于严格或抽象比较,两个不同的对象永远不会相等。
  • An expression comparing Objects is only true if the operands reference the same Object.仅当操作数引用相同的 Object 时,比较对象的表达式才为真。
  • Null and Undefined Types are strictly equal to themselves and abstractly equal to each other. Null 和 Undefined Types 严格相等,抽象上彼此相等。

... ...

If both operands are objects , then JavaScript compares internal references which are not equal when operands refer to different objects in memory.如果两个操作数都是对象,则当操作数引用 memory 中的不同对象时,JavaScript 会比较不相等的内部引用。

Objects are compared by reference.对象通过引用进行比较。 An array is also an object.数组也是 object。 This means [] == [] will always result in false since they are not the exact same object (with the same reference).这意味着[] == []将始终导致false ,因为它们不是完全相同的 object(具有相同的引用)。 This also means [] != [] will always be true for the same reason.这也意味着[] != []出于同样的原因将始终为true

If you want to check for en empty array use the length property:如果要检查 en 空数组,请使用length属性:

<View>
  {recommendations.length ? 
  (
    <Text>Not Empty</Text>
  ) : (
    <Text>Empty</Text>
  )}
</View>

This uses the fact that an empty array has length 0 , which is falsy.这使用了一个空数组的length 0的事实,这是错误的。 If you rather be more explicit recommendations.length != 0 would have the same result.如果您更明确些, recommendations.length != 0会得到相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM