当日历打开时,我需要将背景变灰 - 单击时,我正在尝试编写一个 js 函数并将其与实际的 onclick 处理程序一起调用 - 将其组合起来。但它不起作用,我无法控制 onclick 处理程序,因为我使用的是 react 包?此案如何处理?请帮忙...
class CustomDatePicker extends Component {
handleOpacity(){
// gray out the background page....
alert('test');
}
click(){
this.handleOpacity();
}
render () {
return (
<span>
<button className="custom-datepicker" onClick={this.props.onClick} >
{this.props.value}
</button>
</span>
)
}
}
CustomDatePicker.propTypes = {
onClick: React.PropTypes.func,
value: React.PropTypes.string
}
export default CustomDatePicker;
最佳答案
您可以在 this.click 方法中调用 this.props.onClick 。 您还应该在构造函数方法中绑定(bind) this.click,因为您正在为 React 组件使用 ES2015 类语法。
class CustomDatePicker extends Component {
constructor() {
this.click = this.click.bind(this)
}
handleOpacity(){
// gray out the background page....
alert('test');
}
click(){
this.handleOpacity();
this.props.onClick();
}
render () {
return (
<span>
<button className="custom-datepicker" onClick={this.click} >
{this.props.value}
</button>
</span>
)
}
}
CustomDatePicker.propTypes = {
onClick: React.PropTypes.func,
value: React.PropTypes.string
}
export default CustomDatePicker;
关于reactjs - Reactjs onclick事件调用多个函数-react datepicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396924/