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 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:
7β
8#### Disabling Globally
9β
10[//]: # 'Example'
11β
12```tsx
13//
14const queryClient = new QueryClient({
15 defaultOptions: {
16 queries: {
17 refetchOnWindowFocus: false, // default: true
18 },
19 },
20})
21β
22function App() {
23 return <QueryClientProvider client={queryClient}>...</QueryClientProvider>
24}
25```
26β
27[//]: # 'Example'
28β
29#### Disabling Per-Query
30β
31[//]: # 'Example2'
32β
33```tsx
34useQuery({
35 queryKey: ['todos'],
36 queryFn: fetchTodos,
37 refetchOnWindowFocus: false,
38})
39```
40β
41[//]: # 'Example2'
42β
43## Custom Window Focus Event
44β
45In rare circumstances, you may want to manage your own window focus events that trigger TanStack Query to revalidate. To do this, TanStack 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:
46β
47[//]: # 'Example3'
48β
49```tsx
50focusManager.setEventListener((handleFocus) => {
51 // Listen to visibilitychange
52 if (typeof window !== 'undefined' && window.addEventListener) {
53 window.addEventListener('visibilitychange', () => handleFocus(), false)
54 return () => {
55 // Be sure to unsubscribe if a new handler is set
56 window.removeEventListener('visibilitychange', () => handleFocus())
57 }
58 }
59})
60```
61β
62[//]: # 'Example3'
63[//]: # 'ReactNative'
64β
65## Managing Focus in React Native
66β
67Instead 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":
68β
69```tsx
70import { AppState } from 'react-native'
71import { focusManager } from '@tanstack/react-query'
72β
73function onAppStateChange(status: AppStateStatus) {
74 if (Platform.OS !== 'web') {
75 focusManager.setFocused(status === 'active')
76 }
77}
78β
79useEffect(() => {
80 const subscription = AppState.addEventListener('change', onAppStateChange)
81β
82 return () => subscription.remove()
83}, [])
84```
85β
86[//]: # 'ReactNative'
87β
88## Managing focus state
89β
90[//]: # 'Example4'
91β
92```tsx
93import { focusManager } from '@tanstack/react-query'
94β
95// Override the default focus state
96focusManager.setFocused(true)
97β
98// Fallback to the default focus check
99focusManager.setFocused(undefined)
100```
101β
102[//]: # 'Example4'