美文网首页
react 事件绑定的4种方式

react 事件绑定的4种方式

作者: EdmundChen | 来源:发表于2017-01-02 18:37 被阅读1024次

手动绑定

bind方法

此方法可以绑定事件处理器内的this,并可以向事件方法传参

class test extends Component {
    handleEdit(param) {
        console.log(param)
    }
    
    render() {
        return <button onClick={this.handleEdit.bind(this, param)}>编辑</button>
    }
}
// 如果不传参可用双冒号::
<button onClick={::this.handleEdit}>编辑</button>

构造器内声明


class test extends Component {
    constructor(props){
        super(props);
         this.handleEdit = this.handleEdit.bind(this);
    }
    handleEdit(param) {
        console.log(param)
    }
    
    render() {
        return <button onClick={this.handleEdit}>编辑</button>
    }
}

箭头函数

方法1
class test extends Component {
   handleEdit = (e) => {
        console.log(e)
    }
    
    render() {
        return <button onClick={this.handleEdit}>编辑</button>
    }
}

方法2
class test extends Component {
    handleEdit(param) {
        console.log(param)
    }
    
    render() {
        return <button onClick={() => this.handleEdit(param)}>编辑</button>
    }
}

自动绑定

es5写法, React.createClass

相关文章

  • React学习之漫谈React

    事件系统 合成事件的绑定方式 Test 合成事件的实现机制:事件委派和自动绑定。 React合成事件系统的委托机制...

  • React的合成事件

    React的合成事件:React中声明的事件最终只绑定在document节点上, 通过事件代理的方式来实现事件的操...

  • 2018-10-23react事件系统

    1.合成事件的绑定方式 react事件的绑定方式在写法上与原生的html事件监听器属性很相似,并且含义和触发场景也...

  • (React)绑定事件(this的使用)

    前言 React开发中,给按钮添加点击事件,响应用户的点击 一、绑定事件 按照上述的方式,在事件绑定的方法内部,是...

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

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

  • 025 JS事件

    JS事件 ********* 一、事件的两种绑定方式 ******* 1、on事件绑定方式 2、非on事件绑定方式...

  • react绑定事件的四种写法和理解

    react绑定事件的几种写法和理解 参考react绑定事件的几种写法 - 简书 (jianshu.com)[htt...

  • React事件

    react的事件绑定跟dom元素的事件绑定很相似,不过有些区别: React事件命名是驼峰而不是小写 用JSX语法...

  • 【React进阶系列】史上最全React事件机制详解

    框架总览 ? DOM事件流的三个阶段 ? 关于React事件的疑问? React事件绑定机制? React事件和原...

  • 2018-11-07 react 事件处理

    react事件处理和dom事件处理是相似的。 react: Dom: 所以: React事件绑定属性的命名采用驼峰...

网友评论

      本文标题:react 事件绑定的4种方式

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