【问题标题】:react, setState does not work in my react app反应,setState 在我的反应应用程序中不起作用
【发布时间】:2018-11-04 06:21:33
【问题描述】:

这是我的代码:

import React, { Component } from 'react';

class Counter extends Component {
    constructor(props){
        super(props);
        this.state = {
            number : 0
        };

        this.handleIncrease = this.handleIncrease.bind(this);
        this.handleDecrease = this.handleDecrease.bind(this);
    }

    handleIncrease = () => {
        this.setState=({
            number: this.state.number + 1
        });
        console.log("handleIncrease()");
    }
    handleDecrease = () => {
        this.setState({
            number: this.state.number - 1
        });
        console.log("handleDecrease()");
    }

    render(){
        return (
            <div>
                <hr/>
                <h1>Counter</h1>
                <p>number : {this.state.number}</p>
                <button onClick={this.handleIncrease}>+</button>
                <button onClick={this.handleDecrease}>-</button>
                <hr/>
            </div>
        )
    }
}

export default Counter;

当我按下 - 按钮时它可以工作,但当我按下 + 按钮时它不起作用并出现错误:

TypeError: _this.setState 不是函数 Counter._this.handleDecrease

【问题讨论】:

  • this.setState=(,在handleIncrease函数中删除=之前的(。
  • 谢谢!!!!非常!!!!!!!!!!!!! !!!!!!!!!!!!哇!!!!!!!!!我傻!!!

标签: javascript reactjs setstate


【解决方案1】:

去掉等号:

handleIncrease = () => {
    >>>> this.setState({
       number: this.state.number + 1
     });
     console.log("handleIncrease()");
    }

另外,在使用之前的状态设置新状态时,在 setState 中传递一个函数,因为状态更新可以是异步的。

更多信息:https://reactjs.org/docs/state-and-lifecycle.html

【讨论】:

    【解决方案2】:
    import React, { Component } from 'react';
    
    class Counter extends Component {
        constructor(props){
            super(props);
            this.state = {
                number : 0
            };
    
            this.handleIncrease = this.handleIncrease.bind(this);
            this.handleDecrease = this.handleDecrease.bind(this);
        }
    
        handleIncrease = () => {
           //here remove equal
            this.setState({
                number: this.state.number + 1
            });
            console.log("handleIncrease()");
        }
        handleDecrease = () => {
            this.setState({
                number: this.state.number - 1
            });
            console.log("handleDecrease()");
        }
    
        render(){
            return (
                <div>
                    <hr/>
                    <h1>Counter</h1>
                    <p>number : {this.state.number}</p>
                    <button onClick={this.handleIncrease}>+</button>
                    <button onClick={this.handleDecrease}>-</button>
                    <hr/>
                </div>
            )
        }
    }
    
    export default Counter;
    

    【讨论】:

      猜你喜欢
      • 2017-03-10
      • 2016-05-13
      • 2021-01-29
      • 2019-07-21
      • 2020-03-07
      • 1970-01-01
      • 2021-10-26
      • 2016-08-01
      • 1970-01-01
      相关资源
      最近更新 更多