Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

window-focus-refetching.md

102 lines2.7 KB

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>
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 }
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'