欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

React事件处理的机制及原理

程序员文章站 2023-11-25 13:15:58
react中的事件处理 在react元素中绑定事件有两点需要注意: (1)在react中,事件命名采用驼峰命名方式,而不是dom元素中的小写字母命名方式。例如onc...

react中的事件处理

在react元素中绑定事件有两点需要注意:

(1)在react中,事件命名采用驼峰命名方式,而不是dom元素中的小写字母命名方式。例如onclick要写成onclick,onchange要写成onchange等。
(2)处理事件的响应函数要以对象的形式赋值给事件属性,而不是dom中的字符串形式。例如在dom中绑定一个点击事件应该写成:

<button onclick="clickbutton()">
  click
</button>

而在react元素中绑定一个点击事件变成这种形式:

<button onclick={clickbutton}> // clickbutton是一个函数
  click
</button>

react中的事件是合成事件,并不是原生的dom事件。

react根据w3c规范定义了一套兼容各个浏览器的事件对象。在dom中可以通过返回false来阻止事件的默认行为,但在react中,必须显式的调用事件对象的preventdefault方法来阻止事件的默认行为。

在某些场景下如果必须使用dom提供的原生事件,可以通过react事件对象的nativeevent属性获取。

其实,在平时的开发中,react组件中处理事件最容易出错的地方是事件处理函数中的this的指向问题,因为es6 class并不会为方法自动绑定this到当前对象。

下面我们具体来看一下常见的三种处理this的方式:

react事件处理的this处理

使用箭头函数

直接在react元素中采用箭头函数定义事件的处理函数,如:

class mycomponent extends react.component {
  constructor(props) {
    super(props);
    this.state = {
      number: 0
    }
  }

  render() {
    return (
      <button onclick={(event) => {
          console.log(this.state.number);
        }}>
        click
      </button>
      )
  }
}

箭头函数中的this指向的是函数定义时的对象,所以可以保证this总是指向当前组件的实例对象。

当事件处理逻辑比较复杂时,如果把所有的逻辑直接写在onclick的大括号中,就会导致render函数变的臃肿,不容易直观地看出组件的ui结构,代码可读性也不好。这样,我们可以把逻辑处理封装成组件的一个方法,然后在箭头函数中调用该方法即可。

class mycomponent extends react.component {
  constructor(props) {
    super(props);
    this.state = {
      number: 0
    }
  }

  handleclick(event) {
    const number = ++this.state.number;
    this.setstate({
      number: number
    });

  }

  render() {
    return (
      <button onclick={(event) => {
          this.handleclick(event);
        }}>
        click
      </button>
      )
  }
}

直接在render方法中为元素事件定义事件处理函数,最大的问题是,每次render调用时,都会重新创建一个新的事件处理函数,带来额外的性能开销,组件所处层级越低,这种开销就越大。当然,大多数情况下,这种开销是可以接受的。

使用组件方法

直接将组件的方法赋值给元素的事件属性,同时在类的构造函数中,将这个方法的this绑定到当前对象。如:

class mycomponent extends react.component {
  constructor(props) {
    super(props);
    this.state = {
      number: 0
    };
    this.handleclick = this.handleclick.bind(this);
  }

  handleclick(event) {
    const number = ++this.state.number;
    this.setstate({
      number: number
    });

  }

  render() {
    return (
      <button onclick={this.handleclick}>
        click
      </button>
      )
  }
}

这种方法的好处是每次render不会重新创建一个回调函数,没有额外的性能损失。但在构造函数中,为事件处理函数绑定this,尤其是存在多个事件处理函数需要绑定时,这种模板式的代码还是会显得繁琐。

有时候我们还会为元素的事件属性赋值时,同时为事件处理函数绑定this,例如:

class mycomponent extends react.component {
  
  ……

  render() {
    return (
      /* 事件属性赋值和this绑定同时 */
      <button onclick={this.handleclick.bind(this)}>
        click
      </button>
      )
  }
}

使用bind会创建一个新的函数,因此这种写法依然存在每次render都会创建一个新函数的问题。但是在需要为函数传入额外的参数时,这种写法就比较方便了。

class mycomponent extends react.component {
  
  ……

  render() {
    const type = 1;
    return (
      /* 事件属性赋值和this绑定同时 */
      <button onclick={this.handleclick.bind(this, type)}>
        click
      </button>
      )
  }
}

属性初始化语法

使用es7的property initializers会自动为class中定义的方法绑定this。例如:

class mycomponent extends react.component {
  constructor(props) {
    super(props);
    this.state = {
      number: 0
    };
  }

  handleclick = (event) => {
    const number = ++this.state.number;
    this.setstate({
      number: number
    });

  }

  render() {
    return (
      <button onclick={this.handleclick}>
        click
      </button>
      )
  }
}

这种方式既不需要在构造函数中手动绑定this,也不需要担心组件重复渲染导致的函数重复创建的问题。不过由于property initializers 这个特性还处于试验阶段,默认有些浏览器是不支持的,需要使用babel来进行支持。

通过上面我们可以看到,只要处理好了react组件中函数的this绑定问题,react的事件处理就没有太大的问题了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。