Open History to see the last commit
1---
2id: FocusManager
3title: FocusManager
4redirect_from:
5 - reference/focusManager
6 - framework/react/reference/focusManager
7---
8β
9<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
10β
11Defined in: [packages/query-core/src/focusManager.ts:14](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L14)
12β
13The `FocusManager` manages the focus state within TanStack Query.
14β
15It can be used to change the default event listeners or to manually change the focus state.
16β
17## Extends
18β
19- `Subscribable`\<`Listener`\>
20β
21## Methods
22β
23### hasListeners()
24β
25```ts
26hasListeners(): boolean;
27```
28β
29Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
30β
31Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
32β
33#### Returns
34β
35`boolean`
36β
37`true` if at least one listener is registered.
38β
39#### Inherited from
40β
41```ts
42Subscribable.hasListeners
43```
44β
45***
46β
47### isFocused()
48β
49```ts
50isFocused(): boolean;
51```
52β
53Defined in: [packages/query-core/src/focusManager.ts:130](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L130)
54β
55`isFocused` can be used to get the current focus state.
56β
57#### Returns
58β
59`boolean`
60β
61The focus state set with `setFocused`, or otherwise whether the document is visible.
62β
63***
64β
65### onFocus()
66β
67```ts
68onFocus(): void;
69```
70β
71Defined in: [packages/query-core/src/focusManager.ts:119](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L119)
72β
73`onFocus` notifies all subscribed listeners with the current focus state.
74β
75#### Returns
76β
77`void`
78β
79***
80β
81### setEventListener()
82β
83```ts
84setEventListener(setup: SetupFn): void;
85```
86β
87Defined in: [packages/query-core/src/focusManager.ts:78](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L78)
88β
89`setEventListener` can be used to set a custom event listener that will
90be used to determine the focus state. The provided `setup` function
91receives a `setFocused` callback: call it with a `boolean` to manually
92set the focus state, or with no arguments to re-evaluate the current
93focus state and notify subscribers.
94β
95#### Parameters
96β
97##### setup
98β
99`SetupFn`
100β
101Receives the `setFocused` callback, registers the event listener, and may return
102a cleanup function that is called when the listener is replaced or no longer needed.
103β
104#### Returns
105β
106`void`
107β
108#### Example
109β
110```ts
111import { focusManager } from '@tanstack/query-core'
112β
113focusManager.setEventListener((handleFocus) => {
114 const listener = () => handleFocus()
115 // Listen to visibilitychange
116 if (typeof window !== 'undefined' && window.addEventListener) {
117 window.addEventListener('visibilitychange', listener, false)
118 }
119β
120 return () => {
121 // Be sure to unsubscribe if a new handler is set
122 window.removeEventListener('visibilitychange', listener)
123 }
124})
125```
126β
127***
128β
129### setFocused()
130β
131```ts
132setFocused(focused?: boolean): void;
133```
134β
135Defined in: [packages/query-core/src/focusManager.ts:108](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L108)
136β
137`setFocused` can be used to manually set the focus state. Set `undefined`
138to fall back to the default focus check.
139β
140#### Parameters
141β
142##### focused?
143β
144`boolean`
145β
146The focus state, or `undefined` to use the default focus check.
147β
148#### Returns
149β
150`void`
151β
152#### Example
153β
154```ts
155import { focusManager } from '@tanstack/query-core'
156β
157// Set focused
158focusManager.setFocused(true)
159β
160// Set unfocused
161focusManager.setFocused(false)
162β
163// Fallback to the default focus check
164focusManager.setFocused(undefined)
165```
166β
167***
168β
169### subscribe()
170β
171```ts
172subscribe(listener: Listener): () => void;
173```
174β
175Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
176β
177Registers a listener to be called on every update this object notifies about. Returns a function
178that removes the listener again β call it to stop listening. The base class never drops a listener
179on its own, though some subclasses clear all of theirs in `destroy()`.
180β
181#### Parameters
182β
183##### listener
184β
185`Listener`
186β
187Called on each update, with whatever the subclass passes to its subscribers.
188β
189#### Returns
190β
191A function that removes the listener.
192β
193() => `void`
194β
195#### Example
196β
197```ts
198const unsubscribe = subscribable.subscribe(() => {
199 // react to the update
200})
201β
202unsubscribe()
203```
204β
205#### Inherited from
206β
207```ts
208Subscribable.subscribe
209```