Incrementing state value by one using React

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

    Try this out

    class QuestionList extends React.component {
    
        constructor(props){
            super(props)
            this.state = {
                value : 0
            }
        }
    
        handleClick(){
            this.setState({
                value : this.state.value + 1
            })
        }
    
       render(){
            return(  )
       }
    }
    

    Note that when you set a state, it triggers the render function, which will reflect the current state. Try it out in the browser!

提交回复
热议问题