I understand that react doesn't update state immediately then can someone tell me how i can log this state synchronously as my state is not a boolean and this answer didn't help me setState doesn't update the state immediately. Also i don't understand why after clicking on prev button it increments the value first and then it decrements
class A extends Component {
constructor(props) {
super(props);
this.state = {
value: 1
}
}
handleNext() {
this.setState(prevState => ({
value: prevState.value + 1
}));
console.log(this.state.value);
}
handlePrev() {
if(this.state.value > 1) {
this.setState(prevState => ({
value: prevState.value - 1
}));
}
console.log(this.state.value);
}
render() {
<Button bsStyle="primary" id="prev" onClick={() => this.handlePrev()}>Prev</Button>
<Button bsStyle="primary" id="next" onClick={() => this.handleNext()}>Next</Button>
}