-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(focusManager): stop listening for focus
events
#4805
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,36 +48,19 @@ In rare circumstances, you may want to manage your own window focus events that | |
|
||
```tsx | ||
focusManager.setEventListener((handleFocus) => { | ||
// Listen to visibilitychange and focus | ||
// Listen to visibilitychange | ||
if (typeof window !== 'undefined' && window.addEventListener) { | ||
window.addEventListener('visibilitychange', handleFocus, false) | ||
window.addEventListener('focus', handleFocus, false) | ||
} | ||
|
||
return () => { | ||
// Be sure to unsubscribe if a new handler is set | ||
window.removeEventListener('visibilitychange', handleFocus) | ||
window.removeEventListener('focus', handleFocus) | ||
} | ||
}) | ||
``` | ||
|
||
[//]: # 'Example3' | ||
|
||
## Ignoring Iframe Focus Events | ||
|
||
A great use-case for replacing the focus handler is that of iframe events. Iframes present problems with detecting window focus by both double-firing events and also firing false-positive events when focusing or using iframes within your app. If you experience this, you should use an event handler that ignores these events as much as possible. I recommend [this one](https://gist.github.com/tannerlinsley/1d3a2122332107fcd8c9cc379be10d88)! It can be set up in the following way: | ||
|
||
[//]: # 'Example4' | ||
|
||
```tsx | ||
import { focusManager } from '@tanstack/react-query' | ||
import onWindowFocus from './onWindowFocus' // The gist above | ||
|
||
focusManager.setEventListener(onWindowFocus) // Boom! | ||
``` | ||
|
||
[//]: # 'Example4' | ||
[//]: # 'ReactNative' | ||
|
||
## Managing Focus in React Native | ||
|
@@ -105,7 +88,7 @@ useEffect(() => { | |
|
||
## Managing focus state | ||
|
||
[//]: # 'Example5' | ||
[//]: # 'Example4' | ||
Comment on lines
-108
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should switch to named examples @DamianOsipiuk 😅. I see the vue-query docs only overwrite/use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you know naming things is hard, so i just went with numbers 🙈 |
||
|
||
```tsx | ||
import { focusManager } from '@tanstack/react-query' | ||
|
@@ -117,8 +100,4 @@ focusManager.setFocused(true) | |
focusManager.setFocused(undefined) | ||
``` | ||
|
||
[//]: # 'Example5' | ||
|
||
## Pitfalls & Caveats | ||
|
||
Some browser internal dialogue windows, such as spawned by `alert()` or file upload dialogues (as created by `<input type="file" />`) might also trigger focus refetching after they close. This can result in unwanted side effects, as the refetching might trigger component unmounts or remounts before your file upload handler is executed. See [this issue on GitHub](https://github.com/tannerlinsley/react-query/issues/2960) for background and possible workarounds. | ||
[//]: # 'Example4' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow this is great! I didn't know that it would also fix this issue ❤️