react绑定事件与事件注意事项(后面加续)

1、react可以进行绑定事件,但用的少。
componentDidmount(){
window.addEventListener(‘scroll’,this.scrollEvent);
}
componentWillUnmount(){
this.scrollEvent = ”;
}
2、react事件对象进行绑定。
通过bind可以进行绑定。
class App extends Component{
handleClick(e,arg){
console.log(e,arg);
}
render(){
return(

             <button onClick={this.handleClick.bind(this,'test')}>Test</button>
    )

}

}
如果调用多次handleClick事件,可以进行优化;通过把绑定事件放在构造中只进行一次绑定;
class App extends Component{
constructor(props){
super(props);
this.handleClick=this.handleClick.bind(this,’test’)
}
handleClick(arg,e){
console.log(e,arg);
}
render(){
return(

             <button onClick={this.handleClick}>Test</button>
    )

}

}

猜你喜欢

转载自blog.csdn.net/weixin_42727360/article/details/82191082
今日推荐