T
TanStack3y ago
complex-teal

useQuery keeps fetching data even tho the dependency doesnt change

useQuery keeps fetching data even tho the dependency doesnt change.
const router = useRouter();
const { adid } = router.query;

const query = useQuery("post", () => getData("/post/" + adid, true), { enabled: !!adid });
const router = useRouter();
const { adid } = router.query;

const query = useQuery("post", () => getData("/post/" + adid, true), { enabled: !!adid });
It randomly refetches the data.
4 Replies
complex-teal
complex-tealOP3y ago
Oh, so it refetches data when I change my browser tab and come back, and click on website.
stormy-gold
stormy-gold3y ago
Window Focus Refetching | TanStack Query Docs
If a user leaves your application and returns and the query data is stale, TanStack Query automatically requests fresh data for you in the background. You can disable this globally or per-query using the refetchOnWindowFocus option: Disabling Globally
absent-sapphire
absent-sapphire3y ago
And also by default staleTime is 0 so data are considered stale immediately after being fetched So I suggest to set a staleTime first. And then decide whether or not ton’ disable windows focus refetch which would only happen if data are stale
xenophobic-harlequin
xenophobic-harlequin3y ago
If you're using version 4 your query key should be an array by the way

Did you find this page helpful?