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