react.js事件,捕获类型,事件内部的this,绑定事件写法

react事件理解

  1. 对原生事件进行了封装
  2. 仅仅监听了domcument的所有事件
  3. 根据e.target判断真实触发事件的元素

书写方式:

驼峰式

react事件的捕获类型

  1. 在事件类型上加capture:onClickCapture

react事件内部的this

  1. 箭头函数的this指向调用时的上一级(级组件实例)
  2. 事件监听器的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>

猜你喜欢

转载自blog.csdn.net/Menqq/article/details/108469295
今日推荐