React,state,为什么`count`没有定义?

时间:2016-12-27 05:50:35

标签: reactjs

当我点击“更新'”时,显示此错误。

错误:

  

app.js?7ac9:18 Uncaught ReferenceError: count is not defined

import  React, {Compoment} from 'react';
import ReactDOM from 'react-dom';
class Counter extends  React.Component {
    constructor(props) {
        super(props);
        this.handleClick = this.handleClick.bind(this);
        this.state = {
            count: 0,
        };
    }
    handleClick(e){
        e.preventDefault();
        this.setState({
            count: count + 1,
        });
    }
    render () {
        return(
            <div>
                <p>{this.state.count}</p>
                <a href="#" onClick={this.handleClick}> Update</a>
            </div>
            );
    }
}
ReactDOM.render(<Counter />, document.getElementById('app'))

我在构造函数中使用this.state定义了计数。你在答案中写出了好的代码吗?

1 个答案:

答案 0 :(得分:1)

尝试以下:

handleClick(e){
        e.preventDefault();
        this.setState({
            count: this.state.count + 1,
        });
    }