I have a form that when I click on send it leaves red the field that is empty. It makes different validations in each of the fields. Only when all of the fields are greyed out does it send the data via post and disables the error message.
The problem
Within the handleClick method, it does the state changes if the field is correctly filled, and in the end does a final check on all fields. If everything is gray it sends the data if it does not keep the error. But the first time I click it it sends even though it is wrong, and only the second time I click I discover the error. This is because it is entering my if before making the changes in the fields using setState
. I tried using setTimeout but it did not work and I was told not to be good practice. How do I get async if and only to be enabled when states are upgraded?
if ((valueColor && nameColor && lastNameColor && emailColor
&& cardColor && cvvColor && cpfColor
&& dateColor && periodicityColor) === 'grey') {
console.log('dentro'); this.errorMessage.message = 'none'; //this.handleSubmit();
} else {
console.log('fora'); this.errorMessage.message = 'block';
}