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