T
TanStackโ€ข2y ago
harsh-harlequin

refetchOnWindowFocus not working on in 5.17.15 ?

Hi everyone, I just setup react-query in a new React Vite project, but refetchOnWindowFocus doesn't work for some reason. I tried downgrading to 4.x.x as a sanity check, there it seems to work as expected, is there anything I've missed?
useQuery<User>({
queryKey: getQueryKey(),
queryFn: async ({ signal }) => getUser(signal),
refetchOnWindowFocus: () => {
console.log("refetchOnWindowFocus")
return true
},
})
useQuery<User>({
queryKey: getQueryKey(),
queryFn: async ({ signal }) => getUser(signal),
refetchOnWindowFocus: () => {
console.log("refetchOnWindowFocus")
return true
},
})
I just checked the v4 and v5 sandboxes, and the same issue seems to be present there: v4: https://codesandbox.io/p/devbox/tanstack-query-example-react-simple-cn8h2r v5: https://codesandbox.io/p/devbox/tanstack-query-example-react-simple-lskn7u
3 Replies
stormy-gold
stormy-goldโ€ข2y ago
have you read the upgrade guide?
rival-black
rival-blackโ€ข2y ago
The behavior changed from 4 to 5โ€ฆ (in a very positive way)
harsh-harlequin
harsh-harlequinOPโ€ข2y ago
Aha, well that explains it. ๐Ÿ˜… I thought I checked the migration guide, but I can see now that I did a page search for "refetchOnWindowFocus" which only yielded one answer. For anyone else, the change is documented here: https://tanstack.com/query/latest/docs/react/guides/migrating-to-v5#window-focus-refetching-no-longer-listens-to-the-focus-event And here: https://github.com/TanStack/query/pull/4805 Thank you! ๐Ÿ™‚
GitHub
fix(focusManager): stop listening for focus events by ilyasmez ยท ...
Why? As discussed in #4797, focus event has many caveats, as it's triggered in the following scenarios: When interacting with an iframe. When opening/closing a browser dialog (alert, confirm)....
Migrating to TanStack Query v5 | TanStack Query Docs
Breaking Changes v5 is a major version, so there are some breaking changes to be aware of:

Did you find this page helpful?