onSave=()=>{
if (this.state.intialValue<=0) {
this.setState({errorIntialValue: true})
}
else
{
this.setState({errorIntialValue: false})
}
if (this.state.age>=25|| this.state.age<=-1) {
this.setState({errorAge: true})
}
else{
this.setState({errorAge: false})
}
if (this.state.rollNo<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)
}
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 if(!(this.state.errorIntialValue || this.state.errorAge || errorRollno))
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?