我有一些代码,如下是什么使用返回功能,调用该函数
var React = require('react');
var Controls = React.createClass({
propTypes: {
countdownStatus: React.PropTypes.string.isRequired,
onStatusChange: React.PropTypes.func.isRequired
},
onStatusChange(newStatus) {
return()=>{
this.props.onStatusChange(newStatus);
}
// this.props.onStatusChange(newStatus);
},
render() {
var {countdownStatus} = this.props;
var renderStartStopButton =()=>{
if(countdownStatus == 'started') {
return (
<button className="button secondary"
onClick={this.onStatusChange('paused')}
>Pause</button>
)
} else if(countdownStatus == 'paused') {
return (
<button className="button primary"
onClick={this.onStatusChange('started')}
>Start</button>
)
}
}
return(
<div className="controls">
{renderStartStopButton()}
<button className="button alert hollow"
onClick={this.onStatusChange('stopped')}
>Clear</button>
</div>
)
}
})
module.exports = Controls;
我的问题是在onStatusChange(NEWSTATUS)的功能的不同,这将是错误的,使用其他的注释语句比返回一个匿名函数。但是我无法得到返回函数和直接调用函数的区别。
任何人都可以请帮助解释的区别?非常感谢你!
看到这个https://stackoverflow.com/questions/41688366/react-what-is-this-arrow-function-doing-in-the-props-for-tic-tac-toe-game/41688664#41688664 –
感谢Shubham,现在的工作 –