Not working spreed operator in react useState()

Viewed 13

I want to store the data user state. but the data and res.headers["authorization"] are not assigned in the user state.

If you know the answer please tell me what should i do.

My state

type FormValues = {
  email: string;
  password: string;
  token?: string;
};

 const [user, setUser] = useState<FormValues>({
    email: '',
    password: '',
    token: '',
  });

Submit function

  <form
          action=""
          onSubmit={handleSubmit((data) => {

          console.log(data) //data.email:"xyz@gmail.com" , data.password:"*****"

            setUser({ ...user, email: data.email, password: data.password });

            console.log(user);   //not assign the value

            Api.post('/v1/login', data).then((res) => {

              console.log(res.headers['authorization']);

              setUser({ ...user, token: res.headers['authorization'] });

              console.log(user);   //not assign the value

            }).catch((err)=>{
              console.log(err);
              
            })
            dispatch(addLoginData(user));

            if (!errors.email?.message && !errors.password?.message) {
              navigate('/');
            }
          })}
          className="grid gap-y-2 grid-col"
        >

OutPut

enter image description here

0 Answers
Related