gpt4 book ai didi

javascript - '&&' 运算符用 { this.props.children && React.cloneElement(this.props.children, { foo :this. foo}) 表示什么

转载 作者:行者123 更新时间:2023-12-03 13:25:14 25 4
gpt4 key购买 nike

我有使用 react 路由器渲染的 react 类。我知道 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>
)
}
}

我想了解为什么我们在这里使用“&&”运算符。

最佳答案

这是短路评估

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

它的简写是:

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

关于javascript - '&&' 运算符用 { this.props.children && React.cloneElement(this.props.children, { foo :this. foo}) 表示什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40682064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com