Incrementing state value by one using React

后端 未结 8 1828
不知归路
不知归路 2021-01-30 17:33

In React I am trying to make a button increment a value stored in state. However using the code below function my value is set undefined or NaN when using handleClick.

8条回答
  •  盖世英雄少女心
    2021-01-30 18:34

    This is the shortest code for that. First, initialize the state, then perform a method to increment.

    state = {
        counter: 0
      }
    
    increaseHandler = () => {
        let counter = this.state.counter
        counter += 1
        this.setState({counter: counter})
      }
    

提交回复
热议问题