I have an event handler function that updates some state and then makes a conditional based on that same piece of state. Of course the conditional isn't reflecting the updated state. Is there a way to await the state for changes? If not, how do I achieve this?
async function handleClick(event) {
console.log(values)
// make sure email is not empty
if (values.email.length === 0) {
setErrors({...errors, email: [{msg: "Email can not be empty."}]});
}
// make sure password is not empty
if (values.password.length === 0) {
setErrors((prev) => ({...prev, password: [{msg: "Password can not be empty."}]}));
};
console.log('about to submit: ', errors);
if (errors.email || errors.password) return;
console.log('data is clean');
}