Open History to see the last commit
1---
2id: FocusManager
3title: FocusManager
4---
5β
6The `FocusManager` manages the focus state within TanStack Query.
7β
8It can be used to change the default event listeners or to manually change the focus state.
9β
10Its available methods are:
11β
12- [`setEventListener`](#focusmanagerseteventlistener)
13- [`setFocused`](#focusmanagersetfocused)
14- [`isFocused`](#focusmanagerisfocused)
15β
16## `focusManager.setEventListener`
17β
18`setEventListener` can be used to set a custom event listener:
19β
20```tsx
21import { focusManager } from '@tanstack/react-query'
22β
23focusManager.setEventListener((handleFocus) => {
24 // Listen to visibilitychange
25 if (typeof window !== 'undefined' && window.addEventListener) {
26 window.addEventListener('visibilitychange', handleFocus, false)
27 }
28β
29 return () => {
30 // Be sure to unsubscribe if a new handler is set
31 window.removeEventListener('visibilitychange', handleFocus)
32 }
33})
34```
35β
36## `focusManager.setFocused`
37β
38`setFocused` can be used to manually set the focus state. Set `undefined` to fallback to the default focus check.
39β
40```tsx
41import { focusManager } from '@tanstack/react-query'
42β
43// Set focused
44focusManager.setFocused(true)
45β
46// Set unfocused
47focusManager.setFocused(false)
48β
49// Fallback to the default focus check
50focusManager.setFocused(undefined)
51```
52β
53**Options**
54β
55- `focused: boolean | undefined`
56β
57## `focusManager.isFocused`
58β
59`isFocused` can be used to get the current focus state.
60β
61```tsx
62const isFocused = focusManager.isFocused()
63```