Answer the question
In order to leave comments, you need to log in
Is useState not immediately updated?
My problem is that when I click on the button, the input validation passes and when the validation fails, an error is written to useState and after that I need to find out how many errors. But on the first click, it gives me that 0 errors, although errors are displayed on the page, and if I click the second time, everything is fine
function addFilmHandler() {
Object.keys(form).forEach((key) => setErrors((state) => ({ ...state, [key]: validateField(key, form[key]) })));
console.log(Object.keys(errors).filter((key) => errors[key]).length);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question