0
点赞
收藏
分享

微信扫一扫

React 事件绑定有哪些方法

和谐幸福的人生 2022-02-13 阅读 93

首先在state 设好 " count "

1. 直接在 render 内写行内箭头函数

<button onClick= {() => {

this.setState({count: this.state.count + 1})

}}>点击按钮 + 1</button>

2. 在组件内使用箭头函数定义的一个方法

2-1. 设好箭头函数 addHandler10

addHandler10 = () => {

this.setState({crunt: this.state.crunt + 10})

}

2-2. <button onClick={this.addHandler10}>点击按钮 + 10 </button>

3.直接在组件内定义一个非箭头函数的方法,然后在render里直接使用

3-1.设好函数 addHandler100

addHandler100 () {

this.setState({crunt: this.state.crunt + 100})

}

3-2.    <button onClick={this.addHandler100.bind (this)}>点击按钮 + 100 </button>

4.直接在组件内定义一个非箭头函数的方法,然后在constructor里bind(this)

4-1. 

constructor (props) {
    super(props)
    this.addFn = this.add.bind(this)
  }    

4-2.   <button onClick = { this.addFn }>点击事件加1000</button>

了解到的四种React 事件绑定的方法

举报

相关推荐

0 条评论