Open History to see the last commit
1---
2id: react-native
3title: React Native
4---
5
6React Query is designed to work out of the box with React Native, with the exception of the devtools, which are only supported with React DOM at this time.
7
8There is a 3rd party [Expo](https://docs.expo.dev/) plugin which you can try: https://github.com/expo/dev-plugins/tree/main/packages/react-query
9
10There is a 3rd party [Flipper](https://fbflipper.com/docs/getting-started/react-native/) plugin which you can try: https://github.com/bgaleotti/react-query-native-devtools
11
12There is a 3rd party [Reactotron](https://github.com/infinitered/reactotron/) plugin which you can try: https://github.com/hsndmr/reactotron-react-query
13
14If you would like to help us make the built-in devtools platform agnostic, please let us know!
15
16## Online status management
17
18React Query already supports auto refetch on reconnect in web browser.
19To add this behavior in React Native you have to use React Query `onlineManager` as in the example below:
20
21```tsx
22import NetInfo from '@react-native-community/netinfo'
23import { onlineManager } from '@tanstack/react-query'
24
25onlineManager.setEventListener((setOnline) => {
26 return NetInfo.addEventListener((state) => {
27 setOnline(!!state.isConnected)
28 })
29})
30```
31
32or
33
34```tsx
35import { onlineManager } from '@tanstack/react-query'
36import * as Network from 'expo-network'
37
38onlineManager.setEventListener((setOnline) => {
39 return Network.addNetworkStateListener((state) => {
40 setOnline(state.isConnected)
41 })
42})
43```
44
45## Refetch on App focus
46
47Instead 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":
48
49```tsx
50import { useEffect } from 'react'
51import { AppState, Platform } from 'react-native'
52import type { AppStateStatus } from 'react-native'
53import { focusManager } from '@tanstack/react-query'
54
55function onAppStateChange(status: AppStateStatus) {
56 if (Platform.OS !== 'web') {
57 focusManager.setFocused(status === 'active')
58 }
59}
60
61useEffect(() => {
62 const subscription = AppState.addEventListener('change', onAppStateChange)
63
64 return () => subscription.remove()
65}, [])
66```
67
68## Refresh on Screen focus
69
70In some situations, you may want to refetch the query when a React Native Screen is focused again.
71This custom hook will call the provided `refetch` function when the screen is focused again.
72
73```tsx
74import React from 'react'
75import { useFocusEffect } from '@react-navigation/native'
76
77export function useRefreshOnFocus<T>(refetch: () => Promise<T>) {
78 const firstTimeRef = React.useRef(true)
79
80 useFocusEffect(
81 React.useCallback(() => {
82 if (firstTimeRef.current) {
83 firstTimeRef.current = false
84 return
85 }
86
87 refetch()
88 }, [refetch]),
89 )
90}
91```
92
93In the above code, `refetch` is skipped the first time because `useFocusEffect` calls our callback on mount in addition to screen focus.
94
95## Disable queries on out of focus screens
96
97If you don’t want certain queries to remain “live” while a screen is out of focus, you can use the subscribed prop on useQuery. This prop lets you control whether a query stays subscribed to updates. Combined with React Navigation’s useIsFocused, it allows you to seamlessly unsubscribe from queries when a screen isn’t in focus:
98
99Example usage:
100
101```tsx
102import React from 'react'
103import { useIsFocused } from '@react-navigation/native'
104import { useQuery } from '@tanstack/react-query'
105import { Text } from 'react-native'
106
107function MyComponent() {
108 const isFocused = useIsFocused()
109
110 const { dataUpdatedAt } = useQuery({
111 queryKey: ['key'],
112 queryFn: () => fetch(...),
113 subscribed: isFocused,
114 })
115
116 return <Text>DataUpdatedAt: {dataUpdatedAt}</Text>
117}
118```
119
120When subscribed is false, the query unsubscribes from updates and won’t trigger re-renders or fetch new data for that screen. Once it becomes true again (e.g., when the screen regains focus), the query re-subscribes and stays up to date.