I am trying to check 2 inputs values before handling the submit form. Each time I type a new character, the event returns the previous value isValid is true only if I type
password: 123 and confirmPassword: 1234
I have read this article https://www.freecodecamp.org/news/what-every-react-developer-should-know-about-state/ but I still don't understand why the event has the previous value instead of the current one.
const [password, setPassword] = useState();
const [confirmPassword, setConfirmPassword] = useState();
const [isValid, setValidity] = React.useState<boolean>(false);
const onChangePassword = (event: any) => {
setPassword(event.target.value)
validatePasswords();
console.log(password)
}
const onChangeConfirmPassword = (event: any) => {
setConfirmPassword(event.target.value)
validatePasswords();
console.log(confirmPassword)
}
const validatePasswords = () => {
if (password && confirmPassword) {
setValidity(password == confirmPassword)
console.log('is valid? ', isValid, password, confirmPassword)
}
}