简体   繁体   English

JSX中的虚假值反应

[英]Falsy values in JSX react

I am learning reactJs and trying to pass a property to a component. 我正在学习reactJs并尝试将属性传递给组件。

Code is as follow - 代码如下 -

import React from 'react';
import ReactDOM from 'react-dom';

class myComponent extends React.Component {
    render() {
        if (this.props.signedIn == false) {
            return <h1>Hi</h1>;
        }
        return <h1>Hello!</h1>;
    }
}

ReactDOM.render(
    <myComponent signedIn={false} />,
    document.getElementById('app')
);

This works, but notice the part where I have to inject false as javascript wrapped in curly braces. 这是有效的,但请注意我必须注入false的部分,因为javascript用花括号括起来。

My doubt is that does JSX not recognize 'false' string as falsy value as in normal JS? 我怀疑JSX是不是将'false'字符串识别为普通JS中的虚假值?

Reason for asking- Comparison with ng-show="false" in angular, which hides the element, But as discussed in comment that might be because ng-show directive manually evaluate 'false' as falsy value. 询问的原因 - 与角度中的ng-show =“false”进行比较,隐藏元素,但正如评论中所讨论的那样,可能是因为ng-show指令手动将'false'评估为假值。

Don't forget to fix the component name, it should start with uppercase letters . 不要忘记修复组件名称, 它应该以大写字母开头


The conditions has nothing to do with JSX , as mentioned in other comments and answers. 这些条件与JSX无关,正如其他评论和答案中所提到的那样。 This is just how JavaScript works. 这就是JavaScript的工作原理。

Please Note , important thing to remember: 请注意 ,重要的是要记住:
Never ever do a double equals ( == ) aka "Abstract Equality" against a Boolean, This is asking for bugs. 永远不要对布尔做一个双等于( == )又名“抽象平等” ,这就是要求错误。

Because the engine will do a type coercion only on the Boolean value and this can lead to unexpected behavior. 因为引擎仅对布尔值执行类型强制,这可能导致意外行为。

For example, 例如,

 if(2 == true) //returns false 

and

 if(2 == false) // returns false 

from the spec : 来自规范

If Type(x) is Boolean, return the result of the comparison ! 如果Type(x)是布尔值,则返回比较结果! ToNumber(x) == y. ToNumber(x)== y。

If Type(y) is Boolean, return the result of the comparison x == ! 如果Type(y)是布尔值,则返回比较结果x ==! ToNumber(y). ToNumber(Y)。

Instead, you could do an implicit check: 相反,你可以做一个隐式检查:

 if (this.props.signedIn) 

Or explicit check but use the triple equals aka "strict equality" 或明确检查但使用三等于又名“严格平等”

 if (this.props.signedIn === false) 


As for the react part: Again, it's basically just JavaScript functions and objects after all. 至于react部分:同样,它基本上只是JavaScript函数和对象。
When you are not passing a prop then it will be undefined : 如果你没有通过prop那么它将是undefined

 this.props.signedIn // signedIn will be undefined if we didn't pass it as a prop 

So, an implicit check like mentioned above: 所以,如上所述的隐式检查:

 if (this.props.signedIn) 

Will work just fine. 会工作得很好。


Running example without passing the prop: 运行示例而不传递道具:

 class MyComponent extends React.Component { render() { if (this.props.signedIn) { return <h1>Hi</h1>; } return <h1>not signed on!</h1>; } } ReactDOM.render( <MyComponent />, document.getElementById('root') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

Running example with a false passed in: 传入false运行示例:

 class MyComponent extends React.Component { render() { if (this.props.signedIn) { return <h1>Hi</h1>; } return <h1>not signed on!</h1>; } } ReactDOM.render( <MyComponent signedIn={false}/>, document.getElementById('root') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

Running example with a true passed in: 运行带有true示例:

 class MyComponent extends React.Component { render() { if (this.props.signedIn) { return <h1>Hi</h1>; } return <h1>not signed on!</h1>; } } ReactDOM.render( <MyComponent signedIn={true}/>, document.getElementById('root') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

No Javascript doesn't compare a falsy string with false , if you wish to do that, you could simply do it by checking the if condition and not supplying the falsy boolean value as a prop. 没有Javascript不会将falsy stringfalse进行比较,如果您希望这样做,您可以通过检查if条件并且不提供虚假布尔值作为道具来完成。

class MyComponent extends React.Component {
  render() {
    if (this.props.signedIn) {
      return <h1>Hi</h1>;
    }
    return <h1>Hello!</h1>;
  }
}

render(
  <MyComponent />,
  document.getElementById('root')
);

and a truthy case as 和一个真实的案件

render(
  <MyComponent signedIn/>,
  document.getElementById('root')
);

All false , 0 , empty strings '' and "" , NaN , undefined , and null are always evaluated as false; 所有false0 ,空字符串''""NaNundefinednull总是被评估为false; everything else is true. 其他一切都是真的。

JSX does not provide anything by itself except the templating system. 除模板系统外,JSX本身不提供任何功能。 so its base on javascript and not the JSX. 所以它基于javascript而不是JSX。

so in you'r example you can just remove the == false and javascript will evaluate all those patterns for you. 所以在你的例子中你可以删除== false ,javascript将为你评估所有这些模式。

by the way you can take a look at ng-hide source in this link to learn more 顺便说一下,您可以在此链接中查看ng-hide源以了解更多信息

https://github.com/angular/angular.js/blob/master/src/ng/directive/ngShowHide.js https://github.com/angular/angular.js/blob/master/src/ng/directive/ngShowHide.js

and also learn more about operator here 在此处了解有关运营商的更多信息

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

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