Reactjs 这个代码snipe是做什么的,还是仅仅是一个错误
我学习Reactjs并阅读了大量代码,但我无法理解这一点。Reactjs 这个代码snipe是做什么的,还是仅仅是一个错误,reactjs,react-props,Reactjs,React Props,我学习Reactjs并阅读了大量代码,但我无法理解这一点。 这是代码中的错误还是它做了什么 我是说这句话: this.props.onEnd && this.props.onEnd(); 我知道onEnd()是对父级的回调,但它是如何工作的? 代码 这相当于说: if (this.props.onEnd != null) { // validate not null or undefined this.props.onEnd(); // exec
这是代码中的错误还是它做了什么 我是说这句话:
this.props.onEnd && this.props.onEnd();
我知道onEnd()
是对父级的回调,但它是如何工作的?
代码
这相当于说:
if (this.props.onEnd != null) { // validate not null or undefined
this.props.onEnd(); // execute function
}
当表达如下时:
this.props.onEnd && this.props.onEnd();
如果oned
为空或未定义,则表达式将短路。如果是,则表达式的逻辑计算结果为:
false && this.props.onEnd()
在这种情况下,由于
false&&作为互联网搜索。这相当于说:
if (this.props.onEnd != null) { // validate not null or undefined
this.props.onEnd(); // execute function
}
当表达如下时:
this.props.onEnd && this.props.onEnd();
如果oned
为空或未定义,则表达式将短路。如果是,则表达式的逻辑计算结果为:
false && this.props.onEnd()
在这种情况下,由于false&&作为互联网搜索。我将代码更改为const{onEnd}=this.props;onEnd&&onEnd()代码>然后Eslint又不高兴了“期待一个赋值或函数调用,而看到了一个表达式。Eslint没有未使用的表达式”Eslint不喜欢短路,但我喜欢!lolI将代码更改为const{onEnd}=this.props;onEnd&&onEnd()代码>然后Eslint又不高兴了“期待一个赋值或函数调用,而看到了一个表达式。Eslint没有未使用的表达式”Eslint不喜欢短路,但我喜欢!英雄联盟