Incrementing state value by one using React

后端 未结 8 1844
不知归路
不知归路 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:30

    import React from 'react'
    
    class App extends React.Component{
      constructor(){
        super()
        this.state = {
          count: 0
        }
        this.handleClick = this.handleClick.bind(this)
      }
      handleClick(){
        this.setState(prevState => {
          return {
            count: prevState.count + 1
          }
        })
      }
      render(){
        return(
          

    {this.state.count}

    ) } } export default App

提交回复
热议问题