react-book-examples
react-book-examples copied to clipboard
第21页的计数器组件样例报错
RT,我的React版本是15.4.3,代码照书上敲的,报了两处错误
1、constructor中初始化handleClick方法的作用域绑定失败,报undefined,找不到方法,后来在render里绑定成功,onClick="this.handleClick.bind(this)"
2、 handleClick方法里的加法运算报错,找不到count,后来改成count: this.state.count + 1 后成功
不知道是怎么回事,环境是mac,node版本7.x
constructor 里 super 执行了么?完整的代码是书上的例子?
import React, {Component} from 'react';
class Counter extends Component {
super();
this.handleClick = this.handleClick.bind(this);
this.state = {
count: 0
}
handleClick() {
....
}
render() {
....
}
}
提问的时候需要展示代码、展示 Chrome devtools 里的错误信息,建议题主还是先学习一下 js 的基础。。。。
handleClick(e){ //e.preventDefault(); this.setState({count:this.state.count+1}); } 原来那样写不能用的