I'm trying to validate the phone field, to get all letters and dots with the code below.
validatePhone = () => {
const sanitizedPhone = this.state.phone.replace(/\D/g, '');
if (sanitizedPhone.length >= 10 && sanitizedPhone.length <= 11) {
this.setState({ phone: sanitizedPhone });
return true;
}
toast.error('Invalid phoneNumber.', {
position: "top-center",
autoClose: false,
closeOnClick: true,
});
return false;
}
When i trying console.log(sanitizedPhone) with dots in input like 11.97.4.4.51234 i get 11974451234 but after this, on console.log(this.state.phone) i get the older number 11.97.4.4.51234
setStatedoesn't always update instantly. Console.log in you render method.thisinside it would have the same value just as before you assigned it.