Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

FocusManager.md

202 lines3.9 KB

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```