T
TanStack3y ago
relaxed-coral

Disable query on windows click

How can I disable a query if I visit the browser again? I tried to use refetchOnWindowFocus but that did not work:
const { data: currentSettings, isLoading: isCurrentSettingsLoading } =
api.templateSettings.findById.useQuery(
{
id: router.query.id as string,
},
{
enabled: !!router.query.id,
refetchOnWindowFocus: false,
// This way we are updating the field, so it will be pre-filled
onSuccess: (data) => {
setValue('cssLibrary', data.cssLibrary)
},
}
)
const { data: currentSettings, isLoading: isCurrentSettingsLoading } =
api.templateSettings.findById.useQuery(
{
id: router.query.id as string,
},
{
enabled: !!router.query.id,
refetchOnWindowFocus: false,
// This way we are updating the field, so it will be pre-filled
onSuccess: (data) => {
setValue('cssLibrary', data.cssLibrary)
},
}
)
8 Replies
relaxed-coral
relaxed-coralOP3y ago
If I open the dev tools and click on my page, it always tries to refetch Also how can I make sure to call this route if router.query.id really exists If it does not exists I don't want to fetch this.
exotic-emerald
exotic-emerald3y ago
You're likely seeing https://tanstack.com/query/v4/docs/react/guides/window-focus-refetching. You can disable the query using the enabled option to make it dependant on the ID like you're doing
relaxed-coral
relaxed-coralOP3y ago
But I already set the enabled option
exotic-emerald
exotic-emerald3y ago
Then the query function shouldn't run unless you have a valid ID
genetic-orange
genetic-orange3y ago
How can I disable a query if I visit the browser again?
what do you mean with "visit the browser again" ? There's some ambiguity here ... if you OPEN a browser page, or referesh it, the cache is gone because it lives in memory. maybe that isn' t clear?
exotic-emerald
exotic-emerald3y ago
Good point ☝️ having read it again (whilst not in a rush), that's exactly what it sounds like :reactquery:
relaxed-coral
relaxed-coralOP3y ago
@TkDodo 🔮 So in my case I have the dev console open, if I click inside of the dev console and click back to the actual page, the refetch occurs, that should not be related to cache I think, or am I missing something here?
genetic-orange
genetic-orange3y ago
that's definitely refetchOnWindowFocus. I can see that you've turned it off, so it shouldn't happen. You'd need to show a reproduction please

Did you find this page helpful?