-1

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

  • How exactly are you observing that the state isn't being updated? Can you provide a runnable [mcve] which demonstrates? – David May 24 '22 at 17:27
  • I added a sandbox link – Saghar Francis May 24 '22 at 17:42
  • Please include relevant code and debugging information in the question itself. The question can even be updated to a runnable [mcve] using a code snippet when editing the question. Off-site demonstrations can be useful, but off-site content can change over time and render the question useless. (Additionally... The link you provided is to a default sandbox template. None of your code is there.) – David May 24 '22 at 17:46
  • re-try the link its working – Saghar Francis May 24 '22 at 17:48
  • Does this answer your question? [The useState set method is not reflecting a change immediately](https://stackoverflow.com/questions/54069253/the-usestate-set-method-is-not-reflecting-a-change-immediately) – David May 24 '22 at 17:48

0 Answers0