Please look into the following code. I have tried calling setState one below the other. It looks like count which is returned from the second call of setState is always set to this.state.count. Here, It's always assigned with 3
class Counter extends React.Component {
constructor(props){
super(props);
this.handleReset = this.handleReset.bind(this);
this.state = {
count : 0
};
}
handleReset() {
this.setState(() => {
return {
count: 1
}
});
this.setState(() => {
return {
count: 3
}
});
}
render() {
return (
<div>
<h1>Count:{this.state.count}</h1>
<button onClick={this.handleReset}>reset</button>
</div>
);
}
}
ReactDOM.render(<Counter />, document.getElementById('app'));