What does '&&' operator indicate with { this.props.children && React.cloneElement(this.props.children, { foo:this.foo})

前端 未结 5 1919
悲哀的现实
悲哀的现实 2020-12-15 11:22

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 \'&&

5条回答
  •  再見小時候
    2020-12-15 11:55

    In simple words the purpose of that && is:

    Do not attempt to clone and render children when there are no children.

    So if you use Users like this:

    
       
       
    
    

    then both Child1 and Child2 will get rendered with additional props foo.

    But if the parent is used in this way or , there are no child components to render. So we are performing a check before we invoke React.cloneElement.

    && is equivalent to boolean AND: 1 AND A === A => 1 && A = A

    || is equivalent to boolean OR: 1 OR A = 1 => 1 || A = 1

提交回复
热议问题