[英]Why "0" is rendered on short-circuit evaluation of `array.length && ...`
Currently, I see behavior like this:目前,我看到这样的行为:
render() {
const list = [];
return (
<div>
{ list.length && <div>List rendered</div> }
</div>
)
}
My expected is nothing rendered with that condition, but string "0" rendered (string "0" is list.length
).我的预期是在该条件下没有呈现任何内容,但是呈现了字符串“0”(字符串“0”是
list.length
)。 I don't know why.我不知道为什么。 Anybody can help me explain this case to React?
有人可以帮我向 React 解释这个案例吗?
That's basically the way, short-circuit evaluation is designed:基本上就是这样, 短路评估是设计的:
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
当逻辑表达式从左到右求值时,使用以下规则测试它们可能的“短路”求值:
(some falsy expression) && expr is short-circuit evaluated to the falsy expression
(一些虚假的表达式)&& expr 是对虚假表达式的短路评估
Thus, 0 is returned with the line { list.length && <div>List rendered</div> }
.因此, 0 与行
{ list.length && <div>List rendered</div> }
。
While it is also evaluated as falsy it is not ignored on render as opposed to false
, null
, undefined
or true
.虽然它也被评估为假, 但在渲染时不会被忽略,而不是
false
、 null
、 undefined
或true
。
So, if you want your short-circuit expression to return one of ignored values, you may do it this way:因此,如果您希望短路表达式返回被忽略的值之一,您可以这样做:
{ list.length>0 && <div>List rendered</div> }
Or cast 0
to false
, like that:或者将
0
为false
,如下所示:
{ !!list.length && <div>List rendered</div> }
Following is a quick demo as a proof of concept:以下是作为概念证明的快速演示:
const { render } = ReactDOM const Component = () => { const list = [] return ( <div> <div>Rendered on <code>{`list.length && <div>List rendered</div>`}</code>:{ list.length && <div>List rendered</div> }</div> <div>Rendered on <code>{`list.length>0 && <div>List rendered</div>`}</code>:{ list.length>0 && <div>List rendered</div> }</div> <div>Rendered on <code>{`!!list.length && <div>List rendered</div>`}</code>:{ !!list.length && <div>List rendered</div> }</div> </div> ) } render (<Component />, document.getElementById('root'))
code {background-color: grey; color:white;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script><div id="root"></div>
This isn't related to React really, but rather to JavaScript directly:这实际上与 React 无关,而是与 JavaScript 直接相关:
const output = list.length && "foobar";
If list.length
is falsy , then output
will take its value (so 0
in this case), and not false
as one might think.如果
list.length
为falsy ,则output
将取其值(在本例中为0
),而不是人们可能认为的false
。
I am a bit late to answer it but i would like to express my understandings.我回答得有点晚,但我想表达我的理解。
As other answer by @sp00m explains clearly that it is not related to React but it is a feature of javascript.正如@sp00m 的其他答案清楚地解释了它与React无关,但它是 javascript 的一个特性。
What it does?它能做什么?
When the left side is falsy then it will take the value of it as in your case it will be 0
because of the [].length
.当左侧为假时,它将采用它的值,因为在您的情况下它将为
0
因为[].length
。 This causes to print "0"
in the DOM.这会导致在 DOM 中打印
"0"
。
Solution:解决方案:
You can make it for a truthy expectation like [].length > 0
or [].length !== 0
or !![].length
or make a ternary operator to show the component dom.您可以将其用于
[].length > 0
或[].length !== 0
或!![].length
类的真实期望,或者使用三元运算符来显示组件 dom。
The last one converts the falsy/truthy values to a boolean value.最后一个将虚假/真实值转换为布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.