我有使用反应路由器渲染的反应类。我知道 React.cloneElement 用于将元素从父级传递到子级。但是为什么/“&&”运算符对这种语句执行什么操作:
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>
)
}
}
我想了解为什么我们在这里使用“&&”运算符。
It's 短路评估
(if this part is true) && (this part will execute)
它的简写是:
if(condition){
(this part will execute)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)