title: How to update state in react synchronously
body: <pre><code>onSave=()=&gt;{
      if (this.state.intialValue&lt;=0) {
          this.setState({errorIntialValue: true})
        }
        else
        {
        this.setState({errorIntialValue: false})
        }
        if (this.state.age&gt;=25|| this.state.age&lt;=-1) {
          this.setState({errorAge: true})
        }
        else{
          this.setState({errorAge: false})
        }
        if (this.state.rollNo&lt;0) {
          this.setState({errorRollno: true})
        }
        else{
          this.setState({errorRollno: false})
        }
         if(!(this.state.errorIntialValue|| this.state.errorAge ||errorRollno)){    //have to 
                                                                      enter only if no error
      let newData={
              intialValue:this.state.intialValue,
              age:this.state.age,
              rollNo:this.state.rollNo
      }
      this.props.updateData(newData)
  }
  </code></pre>
  
  <p>I have a onClick event onSave. If error is there in the form I'm setting the state of those to true.Since SetState is asynchronous,the value won't be updated to its state and is always undefined when it come to  <code>if(!(this.state.errorIntialValue || this.state.errorAge || errorRollno))</code> and it returns false. The code in if block will never get executed. 
  I'm not able to find a proper way to achieve this.How can I do this?</p>