TanStack Query in React Native: Refetch When Back Online
TanStack Query refetches the queries on screen when the browser goes back online. In React Native it does not, because the library has nothing that tells it the network has changed. On a phone, a screen that relies on push notifications instead of polling stays stale after a network cut until the user pulls to refresh.
The problem
Section titled “The problem”When an app moves from polling to push, the screens stop asking the server on a timer and only refetch when a push arrives. That works until the phone loses the network:
- A push that never arrives leaves the screen stale.
- The phone comes back online, but nothing triggers a refetch.
refetchOnReconnectistrueby default, but TanStack Query never sees a reconnect.
The cause
Section titled “The cause”TanStack Query tracks connectivity through onlineManager. Its default listener uses the browser’s
online and offline events on window. React Native has no such events, so onlineManager
assumes the device is always online:
- Queries never pause while offline; they fire and fail.
refetchOnReconnectnever fires, because there is never a transition to “online”.
focusManager has the same gap: on the web it listens to visibilitychange, and React Native has
no such event, so refetchOnWindowFocus does nothing there either. This note covers onlineManager
only.
The fix: NetInfo → onlineManager
Section titled “The fix: NetInfo → onlineManager”Install NetInfo. It is a native module, so in an Expo app use npx expo install to get the version
that matches your SDK:
npx expo install @react-native-community/netinfoThen give onlineManager a listener that reports NetInfo’s state:
import NetInfo from "@react-native-community/netinfo";import { onlineManager } from "@tanstack/react-query";import { Platform } from "react-native";
// The web already gets this from the browser's online/offline events.if (Platform.OS !== "web") { onlineManager.setEventListener((setOnline) => NetInfo.addEventListener((state) => { // Null until NetInfo knows: counting that as offline would hold // the first requests at launch. if (state.isConnected !== null) setOnline(state.isConnected); }), );}Import it once, at the top of the root layout:
import "@/lib/app-online";What changes:
- While offline, queries pause instead of failing, and keep their cached data.
- On the offline → online transition, every active query refetches (
refetchOnReconnect).
The side effect: mutations start waiting too
Section titled “The side effect: mutations start waiting too”onlineManager drives mutations as well as queries. The default networkMode for both is
"online", so once TanStack Query knows the device is offline, a mutation started offline
pauses. It doesn’t fail. It stays pending in memory and runs on its own when the network comes
back.
For actions with real-world consequences, that is a bug:
| Moment | What the user sees | What happens |
|---|---|---|
| Taps “Accept” offline | Spinner, then nothing | Mutation paused |
| Moves on to something else | Assumes it didn’t work | Still paused |
| Network returns minutes later | Nothing | The accept goes through |
The same goes for a “Cancel order” tap the user no longer means. Before NetInfo was wired in, these failed right away with a network error, which is the behaviour users expect from a button.
The fix: networkMode: "always" for mutations only
Section titled “The fix: networkMode: "always" for mutations only”Set networkMode on mutations and leave queries alone:
import { QueryClient } from "@tanstack/react-query";
export function createQueryClient(): QueryClient { return new QueryClient({ defaultOptions: { // queries: left on the default networkMode, so reads pause offline // and refetch on reconnect. // An action sent offline fails at once instead of waiting // and going through later without the user noticing. mutations: { networkMode: "always" }, }, });}The two settings are independent:
networkMode |
Offline | Back online | |
|---|---|---|---|
queries (reads) |
"online" (default) |
Paused, cached data kept | Refetch |
mutations (actions) |
"always" |
Run and fail with a network error | Nothing replays |
Reads still catch up on their own. Actions behave like plain fetch: no network, immediate error,
and the user decides whether to retry.
Summary
Section titled “Summary”- React Native: wire
onlineManagerto NetInfo. TanStack Query only tracks connectivity itself in the browser. - Skip NetInfo’s initial
null, so launch isn’t treated as offline. - Wiring
onlineManageralso pauses mutations. Setmutations: { networkMode: "always" }unless you deliberately want offline actions queued. - NetInfo is native: ship it with a new build, not an over-the-air update.