Skip to content

Commit 900d2c5

Browse files
authored
docs: update react native app focus instructions (#4191)
1 parent ec23747 commit 900d2c5

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

docs/guides/window-focus-refetching.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,17 @@ Instead of event listeners on `window`, React Native provides focus information
6767
import { AppState } from 'react-native'
6868
import { focusManager } from '@tanstack/react-query'
6969

70-
focusManager.setEventListener(handleFocus => {
71-
const subscription = AppState.addEventListener('change', state => {
72-
handleFocus(state === 'active')
73-
})
74-
75-
return () => {
76-
subscription.remove()
70+
function onAppStateChange(status: AppStateStatus) {
71+
if (Platform.OS !== 'web') {
72+
focusManager.setFocused(status === 'active')
7773
}
78-
})
74+
}
75+
76+
useEffect(() => {
77+
const subscription = AppState.addEventListener('change', onAppStateChange)
78+
79+
return () => subscription.remove()
80+
}, [])
7981
```
8082

8183
## Managing focus state

docs/react-native.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ onlineManager.setEventListener(setOnline => {
2727

2828
## Refetch on App focus
2929

30-
In React Native you have to use React Query `focusManager` to refetch when the App is focused.
30+
Instead of event listeners on `window`, React Native provides focus information through the [`AppState` module](https://reactnative.dev/docs/appstate#app-states). You can use the `AppState` "change" event to trigger an update when the app state changes to "active":
3131

3232
```ts
33+
import { AppState } from 'react-native'
3334
import { focusManager } from '@tanstack/react-query'
3435

3536
function onAppStateChange(status: AppStateStatus) {

0 commit comments

Comments
 (0)