react事件理解
- 对原生事件进行了封装
- 仅仅监听了domcument的所有事件
- 根据e.target判断真实触发事件的元素
书写方式:
驼峰式
react事件的捕获类型
- 在事件类型上加capture:onClickCapture
react事件内部的this
- 箭头函数的this指向调用时的上一级(级组件实例)
- 事件监听器的this会被处理成undefined
绑定事件写法:
方法一(写法和用法):
constructor(props){
super(props)
this.testEvent=this.testEvent.bind(this)
}
<button onClick={this.testEvent}>点点点</button>
方法二(写法和用法):
testEvent=()=>{
console.log(123,ev,this)
}
<button onClick={this.testEvent}>点点点</button>
方法三(写法和用法):
testEvent(){
console.log(123,ev,this)
}
<button onClick={()=>this.testEvent()}>点点点</button>