React中ES6事件绑定相关事项
2017-04-17 18:08
302 查看
如果使用类似与onClick方法时
一定要写成 onClick={this.click.bind(this)} 否则无效果
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
<
4000
/ul>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
一定要写成 onClick={this.click.bind(this)} 否则无效果
第一种写法: _handleClick(e) { console.log(this); } render() { return ( <div> <h1 onClick={this._handleClick.bind(this)}>点击</h1> </div> ); }1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
<
4000
/ul>
第二种写法: constructor(props) { super(props); this._handleClick = this._handleClick.bind(this) } _handleClick(e) { console.log(this); } render() { return ( <div> <h1 onClick={this._handleClick}>点击</h1> </div> ); }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
第三种写法: _handleClick = (e) => { // 使用箭头函数(arrow function) console.log(this); } render() { return ( <div> <h1 onClick={this._handleClick}>点击</h1> </div> ); }
相关文章推荐
- react事件绑定--不再需要bind啦(es6)
- ListView响应长按事件的相关注意事项
- JS中批量给元素绑定事件过程中的相关问题使用闭包解决
- jQuery的on与bind绑定事件的区别与注意事项
- Visual Event查看页面相关绑定事件
- jQuery后绑定事件注意事项
- Repeater 的操作,数据的显示,绑定,动态取控件中的值和Repeater1_ItemCommand事件的相关用法
- React Native 原生与JS之间事件绑定注册 作用在于原生可以直接调用JS的方法
- ACS 主动发起连接 相关事件和注意事项(TR069协议翻译)
- easyui-combobox绑定回车事件相关
- ExtJS4学习笔记七--事件绑定相关
- 表单提交的相关注意事项,submit-onclick-事件触发执行过程
- 夺命雷公狗jquery---22-bind为jquery对象绑定多个相关事件
- JS 生成的元素事件绑定注意事项
- React:ES6:ES7中的6种this绑定方法
- Repeater 的操作,数据的显示,绑定,动态取控件中的值和Repeater1_ItemCommand事件的相关用法
- 夺命雷公狗jquery---21-bind为jquery对象绑定相关事件
- react 绑定事件
- jQuery中用on绑定事件时需注意的事项
- easyui-combobox绑定回车事件注意事项