react 事件绑定的2种常用方式

方式一:传统

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> ) } }

猜你喜欢

转载自www.cnblogs.com/CyLee/p/9294316.html
今日推荐