Skip to content

Commit b72137c

Browse files
authored
Merge branch 'main' into fix/retry-background-focus-issue
2 parents 51328d4 + 0650eaf commit b72137c

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

docs/framework/react/react-native.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ useEffect(() => {
7474
## Refresh on Screen focus
7575

7676
In some situations, you may want to refetch the query when a React Native Screen is focused again.
77-
This custom hook will call the provided `refetch` function when the screen is focused again.
77+
This custom hook will refetch **all active stale queries** when the screen is focused again.
7878

7979
```tsx
8080
import React from 'react'
8181
import { useFocusEffect } from '@react-navigation/native'
82+
import { useQueryClient } from '@tanstack/react-query'
8283

83-
export function useRefreshOnFocus<T>(refetch: () => Promise<T>) {
84+
export function useRefreshOnFocus() {
85+
const queryClient = useQueryClient()
8486
const firstTimeRef = React.useRef(true)
8587

8688
useFocusEffect(
@@ -90,13 +92,18 @@ export function useRefreshOnFocus<T>(refetch: () => Promise<T>) {
9092
return
9193
}
9294

93-
refetch()
94-
}, [refetch]),
95+
// refetch all stale active queries
96+
queryClient.refetchQueries({
97+
queryKey: ['posts'],
98+
stale: true,
99+
type: 'active',
100+
})
101+
}, [queryClient]),
95102
)
96103
}
97104
```
98105

99-
In the above code, `refetch` is skipped the first time because `useFocusEffect` calls our callback on mount in addition to screen focus.
106+
In the above code, the first focus (when the screen is initially mounted) is skipped because `useFocusEffect` calls our callback on mount in addition to screen focus.
100107

101108
## Disable queries on out of focus screens
102109

0 commit comments

Comments
 (0)