Open History to see the last commit
1---
2id: window-focus-refetching
3title: Window Focus Refetching
4---
5β
6If a user leaves your application and returns to stale data, **React Query automatically requests fresh data for you in the background**. You can disable this globally or per-query using the `refetchOnWindowFocus` option:
7β
8#### Disabling Globally
9β
10```js
11//
12const queryClient = new QueryClient({
13 defaultOptions: {
14 queries: {
15 refetchOnWindowFocus: false,
16 },
17 },
18})
19β
20function App() {
21 return <QueryClientProvider client={queryClient}>...</QueryClientProvider>
22}
23```
24β
25#### Disabling Per-Query
26β
27```js
28useQuery('todos', fetchTodos, { refetchOnWindowFocus: false })
29```
30β
31## Custom Window Focus Event
32β
33In rare circumstances, you may want to manage your own window focus events that trigger React Query to revalidate. To do this, React Query provides a `focusManager.setEventListener` function that supplies you the callback that should be fired when the window is focused and allows you to set up your own events. When calling `focusManager.setEventListener`, the previously set handler is removed (which in most cases will be the default handler) and your new handler is used instead. For example, this is the default handler:
34β
35```js
36focusManager.setEventListener(handleFocus => {
37 // Listen to visibilitychange and focus
38 if (typeof window !== 'undefined' && window.addEventListener) {
39 window.addEventListener('visibilitychange', handleFocus, false)
40 window.addEventListener('focus', handleFocus, false)
41 }
42β
43 return () => {
44 // Be sure to unsubscribe if a new handler is set
45 window.removeEventListener('visibilitychange', handleFocus)
46 window.removeEventListener('focus', handleFocus)
47 }
48})
49```
50β
51## Ignoring Iframe Focus Events
52β
53A great use-case for replacing the focus handler is that of iframe events. Iframes present problems with detecting window focus by both double-firing events and also firing false-positive events when focusing or using iframes within your app. If you experience this, you should use an event handler that ignores these events as much as possible. I recommend [this one](https://gist.github.com/tannerlinsley/1d3a2122332107fcd8c9cc379be10d88)! It can be set up in the following way:
54β
55```js
56import { focusManager } from 'react-query'
57import onWindowFocus from './onWindowFocus' // The gist above
58β
59focusManager.setEventListener(onWindowFocus) // Boom!
60```
61β
62## Managing Focus in React Native
63β
64Instead of event listeners on `window`, React Native provides focus information through the [`AppState` module](https://reactnative.dev/docs/appstate#app-states). You can use the `AppState` "change" event to trigger an update when the app state changes to "active":
65β
66```js
67import { AppState } from 'react-native'
68import { focusManager } from 'react-query'
69β
70focusManager.setEventListener(handleFocus => {
71 const subscription = AppState.addEventListener('change', state => {
72 handleFocus(state === 'active')
73 })
74β
75 return () => {
76 subscription.remove()
77 }
78})
79```
80β
81## Managing focus state
82β
83```js
84import { focusManager } from 'react-query'
85β
86// Override the default focus state
87focusManager.setFocused(true)
88β
89// Fallback to the default focus check
90focusManager.setFocused(undefined)
91```
92β
93## Pitfalls & Caveats
94β
95Some browser internal dialogue windows, such as spawned by `alert()` or file upload dialogues (as created by `<input type="file" />`) might also trigger focus refetching after they close. This can result in unwanted side effects, as the refetching might trigger component unmounts or remounts before your file upload handler is executed. See [this issue on GitHub](https://github.com/tannerlinsley/react-query/issues/2960) for background and possible workarounds.