'&&' 是什么意思运算符用 { this.props.children && 表示React.cloneElement(this.props.children, { foo:this.foo}) [英] What does '&&' operator indicate with { this.props.children && React.cloneElement(this.props.children, { foo:this.foo})

查看:51
本文介绍了'&&' 是什么意思运算符用 { this.props.children && 表示React.cloneElement(this.props.children, { foo:this.foo})的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用反应路由器呈现的反应类.我知道 React.cloneElement 用于将元素从父级传递给子级.但是为什么/什么是&&"运算符使用这种语句:

class 用户扩展 React.Component {getInitialState() {返回 {页数:0}},富(){this.setState({'page':1})}使成为() {返回 (<div><h2>用户</h2>{ this.props.children &&React.cloneElement(this.props.children, {foo:this.foo})

)}}

我想了解我们为什么使用&&"操作员在这里.

解决方案

短路评估

(如果这部分是真的)&&(这部分会执行)

它的简写:

if(condition){(这部分会执行)}

I have react class that is rendered using react router. I understand that React.cloneElement is used to pass elements from parent to child. But why/what does the '&&' operator do with this kind of statement :

class Users extends React.Component {
    getInitialState() {
      return {
          page:0
        }
     },      
    foo(){
        this.setState({'page':1})
     }
      render() {
        return (
          <div>
            <h2>Users</h2>
            { this.props.children && React.cloneElement(this.props.children, {
    foo:this.foo})
          </div>
        )
      }
    }

I would like to understand why are we using '&&' operator here.

解决方案

It's Short circuit evaluation

(if this part is true) && (this part will execute)

And it shorthand of:

if(condition){
   (this part will execute)
}

这篇关于'&amp;&' 是什么意思运算符用 { this.props.children &amp;&amp; 表示React.cloneElement(this.props.children, { foo:this.foo})的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆