react-book-examples icon indicating copy to clipboard operation
react-book-examples copied to clipboard

第21页的计数器组件样例报错

Open kaindy7633 opened this issue 7 years ago • 2 comments

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

kaindy7633 avatar Mar 26 '17 09:03 kaindy7633

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 的基础。。。。

Phinome avatar May 13 '17 00:05 Phinome

handleClick(e){ //e.preventDefault(); this.setState({count:this.state.count+1}); } 原来那样写不能用的

shesio avatar Dec 25 '17 08:12 shesio