Reactjs事件处理的机制原理及两大注意事项

2018-12-0321:12:00WEB前端开发Comments2,038 views字数 2694阅读模式

React中的事件处理文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

在React元素中绑定事件有两点需要注意:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

(1)在React中,事件命名采用驼峰命名方式,而不是DOM元素中的小写字母命名方式。例如onclick要写成onClick,onchange要写成onChange等。
(2)处理事件的响应函数要以对象的形式赋值给事件属性,而不是DOM中的字符串形式。例如在DOM中绑定一个点击事件应该写成:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

<button onclick="clickButton()">
  Click
</button>

而在React元素中绑定一个点击事件变成这种形式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

<button onClick={clickButton}> // clickButton是一个函数
  Click
</button>

React中的事件是合成事件,并不是原生的DOM事件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

React根据W3C规范定义了一套兼容各个浏览器的事件对象。在DOM中可以通过返回false来阻止事件的默认行为,但在React中,必须显式的调用事件对象的preventDefault方法来阻止事件的默认行为。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

在某些场景下如果必须使用DOM提供的原生事件,可以通过React事件对象的nativeEvent属性获取。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

其实,在平时的开发中,React组件中处理事件最容易出错的地方是事件处理函数中的this的指向问题,因为ES6 class并不会为方法自动绑定this到当前对象。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

下面我们具体来看一下常见的三种处理this的方式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

React事件处理的this处理文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

使用箭头函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

直接在React元素中采用箭头函数定义事件的处理函数,如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

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总是指向当前组件的实例对象。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

当事件处理逻辑比较复杂时,如果把所有的逻辑直接写在onClick的大括号中,就会导致render函数变的臃肿,不容易直观地看出组件的UI结构,代码可读性也不好。这样,我们可以把逻辑处理封装成组件的一个方法,然后在箭头函数中调用该方法即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

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调用时,都会重新创建一个新的事件处理函数,带来额外的性能开销,组件所处层级越低,这种开销就越大。当然,大多数情况下,这种开销是可以接受的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

使用组件方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

直接将组件的方法赋值给元素的事件属性,同时在类的构造函数中,将这个方法的this绑定到当前对象。如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

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,尤其是存在多个事件处理函数需要绑定时,这种模板式的代码还是会显得繁琐。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

有时候我们还会为元素的事件属性赋值时,同时为事件处理函数绑定this,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

class MyComponent extends React.Component {
  
  ……

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

使用bind会创建一个新的函数,因此这种写法依然存在每次render都会创建一个新函数的问题。但是在需要为函数传入额外的参数时,这种写法就比较方便了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

class MyComponent extends React.Component {
  
  ……

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

属性初始化语法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

使用ES7的property initializers会自动为class中定义的方法绑定this。例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

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来进行支持。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

通过上面我们可以看到,只要处理好了React组件中函数的this绑定问题,React的事件处理就没有太大的问题了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/8507.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/8507.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定