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