I am currently trying to make a request to my API using React Query. When I log my data inside the component function scope it logs fine and gives the data I expect, it also logs the correct data before the handleSubmit does. When I log my data variable inside handleSubmit, after submitting, it always logs undefined.
// useSignup.ts
import axios from "axios";
import { useMutation } from "react-query";
const signup = (credidentials: any) => {
return axios.post("/api/signup", credidentials);
};
const useSignup = () => {
return useMutation(signup);
};
export default useSignup;
// Signup.tsx
const {
mutateAsync: makeApiRequest,
isLoading,
isSuccess,
isError,
data: authData,
} = useSignup();
console.log(authData); // Updates fine and gives the correct data
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
await makeApiRequest(input);
console.log(authData); // Always undefined
};