I'm using useQuery inside a custom hook, called usePostData. this is the custom hook code:
import { useQuery } from "@tanstack/react-query";
import axios from "axios";
const usePostData = (queryKey, endPoint, object) =>
useQuery(
[queryKey],
async () => {
await axios
.post("http://localhost:1337" + endPoint, object)
.then((data) => console.log(data, queryKey))
.catch((err) => console.log(err));
return data;
},
{ enabled: false }
);
export default usePostData;
I'm calling it on a submit event of a form, like this:
const { data: response, refetch } = usePostData(
`postSingularExpense-${Math.random()}`,
"/api/expences",
submittedExpense
);
const handleSubmitExpense = async (e) => {
e.preventDefault();
await refetch();
};
for some reason, it runs 4 times instead of once and thenm stops with this error message:
why is that and how to correct it?