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 visibillitychange 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 AppState.addEventListener('change', handleFocus)
72β
73 return () => {
74 AppState.removeEventListener('change', handleFocus)
75 }
76})
77```
78β
79## Managing focus state
80β
81```js
82import { focusManager } from 'react-query'
83β
84// Override the default focus state
85focusManager.setFocused(true)
86β
87// Fallback to the default focus check
88focusManager.setFocused(undefined)
89```