首页 > 技术文章 > react 事件绑定的2种常用方式

CyLee 2018-07-11 14:30 原文

方式一:传统

import React, { Component } from 'react';

class App extends Component {
  handleSubmit (e, args) {
    console.log(e, args)
  }

  render () {
    return (
      <div onClick={this.handleSubmit.bind(this, 'test')}>test</div>
    )
  }
}

 

2、es6 箭头函数

import React, { Component } from 'react';

class App extends Component {
  handleSubmit = (e) => {
     console.log(e);
  }

  render () {
    return (
      <div onClick={this.handleSubmit}>test</div>
    )
  }
}

// 或者 import React, { Component } from
'react'; class App extends Component { handleSubmit (e) { console.log(e); } render () { return ( <div onClick={ () => this.handleSubmit }>test</div> ) } }

 

推荐阅读