美文网首页
react中绑定this

react中绑定this

作者: any_5637 | 来源:发表于2019-11-04 17:05 被阅读0次

在开发过程中,react定义的组件中,如果不为事件处理程序绑定this:

  import React    from 'react';
  import ReactDOM from 'react-dom';

  export default class ThisTest extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'hh',
    }
  }
  onClick() {
    console.log('点我');
    console.log(this.state.name);
    console.log(this);
  }
  render() {
    return (
    <div>
      <button onClick={this.onClick}>点我</button>
    </div>
    )
  }
  }
image.png

当我点击按钮,页面将报错,我将this打印出来,this的值为‘undefined’,onClick方法没有this的上下文,因此需要我们手动的给它绑定this。通过我的了解,总结了一下几个方式来绑定this:

render方法调用的时候使用.bind(this)进行绑定

  import React    from 'react';
  import ReactDOM from 'react-dom';

  export default class ThisTest extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        name: 'hh',
      }
    }
    onClick() {
      console.log(this.state.name);
      console.log(this);
    }
    render() {
      return (
        <div>
          <button onClick={this.onClick.bind(this)}>使用bind方式绑定this</button>
        </div>
      )
    }
  }

绑定this之后,这里this指向整个组件对象实例。


image.png

render方法调用的时候使用箭头函数进行绑定

使用箭头函数自动绑定了定义此函数作用域的this,而不需要我们手动的使用bin绑定。

  <div>
    <button onClick={() =>this.onClick()}>使用箭头函数绑定this</button>
  </div>

构造器内声明

通过在构造器中给时间处理程序绑定this,可以获得整个dom实例。这种绑定方式的好处在于仅需要进行一次绑定,而不需要每次调用事件监听器时去执行绑定操作。

  import React    from 'react';
  import ReactDOM from 'react-dom';

  export default class ThisTest extends React.Component {
    constructor(props) {
      super(props);
      this.onClick = this.onClick.bind(this);
      this.state = {
        name: 'hh',
      }
    }
    onClick() {
      console.log(this.state.name); 
      console.log(this);
    }
    render() {
      return (
        <div>
          <button onClick={() =>this.onClick()}>在构造器内绑定this</button>
        </div>
      )
    }
  }

使用lodash-decorators的Bind()方法绑定

首先需要将项目支持装饰器:
在根目录下面新建.babelr文件,写入:

  {
    "presets": ["@babel/preset-env"],
    "plugins": [
      ["@babel/plugin-proposal-decorators", { "legacy": true }],
      ["@babel/plugin-proposal-class-properties", { "loose": true }]
    ]
  }

然后就是导入的方法使用

  // 导入lodash-decorators下的Bind方法
  import { Bind } from 'lodash-decorators';

  // 在函数的头上使用bind方法,就会将this指向组件实例
  @Bind()
  onClick(event){
    console.log(this);
  }
  
  <button onClick={this.onClick}>点我</button>

相关文章

  • react事件、生命周期

    事件 react中、原生事件绑定丢失this,绑定this写法 jsx中onClosed={ this.handl...

  • 学习react总结知识点

    传统HTML中 handleclick函数自动绑定了this,而react中 需要手动绑定,下面是回调函数绑定...

  • react中绑定this

    在开发过程中,react定义的组件中,如果不为事件处理程序绑定this: 当我点击按钮,页面将报错,我将this打...

  • react中的this

    在ES6写法中的react:React.Component创建组件,其成员不会自动绑定this,需要手动绑定。手动...

  • #React-Redux

    React-Redux是Redux的官方React绑定库。它能够使你的React组件从Redux store中读取...

  • Vue和React数据绑定对比

    在数据绑定上来说,vue的特色是双向数据绑定,而在react中是单向数据绑定。 一 单向和双向数据绑定其实不是完全...

  • React中的this绑定

    在编写react组件时经常需要进行函数的this绑定,使用ES6方式创建的组件中的函数必须手动绑定this,thi...

  • react中this的绑定

    JavaScript中this的绑定是头疼的问题,于是出现了call,apply,bind三兄弟用来改变函数调用时...

  • Vue.js数据双向绑定实现

    目前的几种主流前端框架中,react是单向绑定,而angular.js和vue.js是双向绑定,实现双向绑定的方法...

  • React事件绑定this的几种方法

    React事件处理函数绑定this的集中方法 Follow me on GitHub React事件处理函数绑定t...

网友评论

      本文标题:react中绑定this

      本文链接:https://www.haomeiwen.com/subject/aopdbctx.html