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