I am using react and typescript. I have a form in which I am setting the values into one useState and in another useState I am setting the password and confirm password field. After validating the password I am updating the other useState password field. I put the debugger in browser source there I found that I am getting the value but the value is not setting in the first useState.
const [userDetail, setUserDetail] = useState<User>([]);
const [usePassword, setUserPassword] = useState<Password>([]);
type User = {
username:String,
email:string,
password:string
}
type Password = {
password:string,
confirmPassword:string
}
const handleFormFieldChange(event:any){
const value = event.target.value;
setUserDetail({...userDetail, [event.target.name]:value})
}
using same kind of method to set the password fields in userPassword state. I am getting values in both states but before sending the response I am validating the password and then setting the password in userDetail state.
const onFormSubmit = () => {
if(condition){
setUserDetail({ ...userDetail, password: usePassword.password})
// the above line is not updating the password in userDetail
}
}
sandbox link for demo