【问题标题】:React, state, Why the `count` is not defined?反应,状态,为什么没有定义“计数”?
【发布时间】:2017-05-11 10:01:54
【问题描述】:

当我单击“更新”时,会显示此错误。

错误:

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 定义了计数。你在答案中写了好的代码吗?

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    尝试关注:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-21
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多