react-事件绑定

来源:互联网 发布:mac解压软件哪个好 编辑:程序博客网 时间:2024/05/18 00:45

事件

onClick点击事件

  • JSX中,onClick={this.函数名}来绑定事件
  • this引用的问题,组件使用ES6写法,需要在构造函数里用bind绑定this,或用箭头函数

改变this的三种方法

1.通过箭头函数

onClick={()=>this.addName()}

2.使用bind绑定this到当前类中

onClick={this.addName.bind(this)

3.在construct构造函数中使用bind

class Register extends React.Component {  constructor(props) {    super(props)    this.login = this.login.bind(this)  }  login() {     ...  }    render () {      return (        <Button onClick={this.login} type='primary'>登录</Button>        )  }}