so I am just learning RTK query and I am trying to just dispatch an action after I run a mutation hook in a form. My code looks like this:
const Login = () => {
const {
register,
handleSubmit,
watch,
formState: { errors },
} = useForm();
const [postCredentials, { isSuccess }] = useLoginMutation();
const dispatch = useDispatch();
const onSubmit = async (data: any) => {
await postCredentials({
username: data.username,
password: data.password,
});
console.log('RESULT', isSuccess);
// {
// isSuccess && dispatch(setUserStatus({ isLoggedIn: true }));
// }
};
---------------
When I submit the form it does post to the end point and responds successfully(network tab in Chrome dev tools). My problem is that after the await postCredentials() function the console.log(isSuccess) is false even after I know the POST is successful. If I click again then the console.log(isSuccess) is true. Any ideas what I am doing wrong? I expect the isSuccess to be true first time around so I can then dispatch a new action. TIA