Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing state value by one using React

Tags:

reactjs

state

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.

class QuestionList extends React.Component {   constructor(props) {     super(props);     this.state = {value: 0};      // This binding is necessary to make `this` work in the callback     this.handleClick = this.handleClick.bind(this);   }     handleClick = (prevState) => {     this.setState({value: prevState.value + 1});     console.log(this.state.value)   } 

Can you tell me why this is happening? it should be correct according to the docs here: https://facebook.github.io/react/docs/state-and-lifecycle.html

like image 913
dwigt Avatar asked Feb 21 '17 10:02

dwigt


People also ask

How do you increment a value on a button click in React?

First of all you need to store the current like amount in the local state of the component. This way you can just display the current amount of likes and react will automatically update the display if you change the buttonLikes amount.

How do you change the value of a state in React?

To change a value in the state object, use the this. setState() method. When a value in the state object changes, the component will re-render, meaning that the output will change according to the new value(s).

Can we directly update state in React?

One should never update the state directly because of the following reasons: If you update it directly, calling the setState() afterward may just replace the update you made. When you directly update the state, it does not change this.


1 Answers

Because you are using the handleClick function incorrectly. Here:

handleClick = (prevState) => { .... } 

prevState will be an event object passed to handleClick function, you need to use prevState with setState, like this:

handleClick = () => {     this.setState(prevState => {        return {count: prevState.count + 1}     }) } 

Another issue is, setState is async so console.log(this.state.value) will not print the updated state value, you need to use callback function with setState.

Check more details about async behaviour of setState and how to check updated value.

Check the working solution:

class App extends React.Component {        constructor(props){         super(props);         this.state={ count: 1}     }       onclick(type){        this.setState(prevState => {           return {count: type == 'add' ? prevState.count + 1: prevState.count - 1}        });    }       render() {      return (        <div>          Count: {this.state.count}          <br/>          <div style={{marginTop: '100px'}}/>          <input type='button' onClick={this.onclick.bind(this, 'add')} value='Inc'/>          <input type='button' onClick={this.onclick.bind(this, 'sub')} value='Dec'/>         </div>       )     }  }    ReactDOM.render(    <App />,    document.getElementById('container')  );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>    <div id='container'></div>
like image 145
Mayank Shukla Avatar answered Oct 03 '22 03:10

Mayank Shukla