简体   繁体   English

在 JSX 代码块中使用 AND 运算符

[英]Using AND operator inside JSX block of code

In one of the React function component code, I see the following;在 React function 组件代码之一中,我看到以下内容;

const [fetched, setFetched] = useState(false);

{fetched && (
            <Table
              states={states}
            />
          )}

Now my question is if this is some kind of commonly used pattern where it seems like only if "fetched" is true, does it go to the second expression and render the Table component?现在我的问题是,如果这是否是某种常用模式,似乎只有“获取”为真,它是 go 到第二个表达式并呈现 Table 组件吗?

Yes, that is correct.对,那是正确的。 Just as you can assign a value to a variables if another variable is falsy, in the following case, assuming that falsyValue is falsy (null, undefined...) the OR operator will catch that as false, but because "this value instead" is a truthy value then that's the value that will be assigned.就像如果另一个变量是假的,你可以为一个变量分配一个值一样,在以下情况下,假设falsyValue是假的(null,undefined...),OR 运算符会将其捕获为假,但是因为“这个值而不是”是一个真实值,那么这就是将被分配的值。

On the other hand, if falsyValue is truthy, then because there's an OR operator there's no need to continue (given that only 1 value has to be true) so the value that falsyValue contains will be assigned to value .另一方面,如果falsyValue是真的,那么因为有一个 OR 运算符,所以不需要继续(假设只有 1 个值必须为真),所以falsyValue包含的值将被分配给value

const value = falsyValue || "this value instead"

You can also do你也可以做

isValid && myFunction()

myFunction will only be executed if isValid is true myFunction仅在isValid为 true 时才会执行

same goes for JSX, JSX 也是如此,

return (
  <div>
    {fetched && (
      <Table
        states={states}
      />
    )}
  </div>
)

This will return an empty <div></div> unless fetched is true/truthy这将返回一个空的<div></div>除非fetched是 true/truthy

Yes that is exactly true and it's very common pattern in JSX.是的,这是完全正确的,它是 JSX 中非常常见的模式。 It's based around how && works.它基于&&的工作方式。

This operator evaluates the first operand using ToBoolean :此运算符使用ToBoolean计算第一个操作数:

  • if the first operand is evaluated as false , this operand is returned如果第一个操作数被评估为false ,则返回此操作数
  • if the first operand is evaluated as true , the second operand is returned如果第一个操作数被评估为true ,则返回第二个操作数

Yes exactly, && does NOT return boolean values.是的, &&不返回 boolean 值。 It returns one of it's operands.它返回它的操作数之一。 Why this also works in if statements as you would expect is because the returned operand is implicitly coerced into a boolean value there.为什么这也适用于您所期望的if语句是因为返回的操作数被implicitly coerced转换为 boolean 值。

Knowing all this, you can use && operator to conditionally render a piece of JSX by using it as a second operand.了解了这一切后,您可以使用&&运算符通过将 JSX 用作第二个操作数来有条件地渲染它。

If you are interested in more about this topic, I would recommend: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators如果您对此主题感兴趣,我会推荐: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

Yes that is a commonly used pattern and it will not return the JSX Table element unless fetched is truthy.是的,这是一种常用的模式,它不会返回 JSX Table 元素,除非fetched是真的。 The React documentation goes over many ways to have conditional logic in your JSX code: https://reactjs.org/docs/conditional-rendering.html . React 文档介绍了多种在 JSX 代码中使用条件逻辑的方法: https://reactjs.org/docs/conditional-rendering.html

Something to note that the other answers did not address is that a value like 0 is falsy, so if it is used with an && operator it will return 0, which is valid JSX element content and it will be rendered so you have to be careful with that.需要注意的是,其他答案没有解决的是像 0 这样的值是虚假的,所以如果它与 && 运算符一起使用,它将返回 0,这是有效的 JSX 元素内容,它将被渲染,所以你必须小心接着就,随即。

In this example below, <div>0</div> will be rendered by the render method:在下面的这个例子中, <div>0</div>将被 render 方法渲染:

render() {
    const count = 0;  
    return (
        <div>
            { count && <h1>Messages: {count}</h1> }
        </div>
    );
}

The falsy values of 0 & NaN will be rendered as the JSX element's content, but the falsy values of false , null , undefined , and "" will cause nothing to be rendered as the JSX element's content. 0NaN的虚假值将被呈现为 JSX 元素的内容,但falsenullundefined""的虚假值将导致没有任何内容被呈现为 JSX 元素的内容。

Here is a little more information on falsy values in javascript: https://developer.mozilla.org/en-US/docs/Glossary/Falsy .以下是有关 javascript 中的虚假值的更多信息: https://developer.mozilla.org/en-US/docs/Glossary/Falsy

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

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