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