React Native
Refetch when a React Native app returns to the foreground or comes back online.
Queries, mutations and hooks work in React Native as they do on the web. The one difference is where the client's focus and online signals come from. React Native has no browser focus or online events, so you give the client signals built from React Native's own modules.
The examples import api.ts from Getting started.
Before you begin
You need React set up as Installation shows, and NetInfo, which reports whether the device is online. Effetch Query doesn't depend on it, so add it yourself:
pnpm add @react-native-community/netinfoGive the client React Native's signals
Pass AppState and NetInfo to reactNativeSignals, and its result to the
client's signals option:
import { } from "@monarcode/effetch-query";
import { } from "@monarcode/effetch-query/react-native";
import from "@react-native-community/netinfo";
import { } from "react-native";
import { } from "./api.js";
export const = .({
: ,
: ({ , }),
});Focus is the app coming back to the foreground: AppState changing to
"active" from anything else. When it does, the stale queries in use
refetch, unless their refetchOnFocus is off. Online follows NetInfo's
isConnected, where null (no connection state yet) counts as online.
When the device comes back online, the stale queries in use refetch too,
unless their refetchOnReconnect is off. Being offline pauses nothing: a
fetch while the device is offline fails, as
Limitations says.
Without these signals, the client uses the browser's, which never change without a DOM, so nothing would refetch on focus or reconnect.
A few things to know:
- You pass React Native in. The package doesn't depend on
react-nativeor NetInfo.reactNativeSignalstakes anything that fitsAppStateLikeandNetInfoLike, so a test can pass stand-ins, such as an app that's always in the foreground. - Listeners start when they're needed. Each client starts its own listeners when one of its queries first needs them, and stops them when no cached query needs them, or when the client is disposed. So one set of signals can serve several clients.
Pause the screens under the one in front
A stack navigator keeps the screens under the one in front mounted, so
their queries keep refetching out of sight. Pause them while they're
hidden, as Pause screens you can't see shows with
React Navigation's useIsFocused.
Refresh and load more from FlatList
FlatList covers the two list gestures:
- For pull to refresh, pass
useRefetch'sisRefetchingandrefetchto itsrefreshingandonRefresh: see Refetch on demand. - For an infinite list, call
fetchNextPagefrom itsonEndReached: see Load more.
Other platforms
For a platform that's neither a browser nor React Native, build the
signals yourself and pass them as the client's signals option: see
Focus and online signals.
Families on older Hermes
A family holds its queries weakly, which needs the runtime's WeakRef and
FinalizationRegistry. Hermes has FinalizationRegistry from release
260318099.0.0. React Native 0.88, for example, ships Hermes
260318099.0.2, which has both. To check your app, log
typeof FinalizationRegistry: "undefined" means it doesn't have it.
On an older Hermes, a family keeps a small definition (about 2 KB,
measured on V8) for every argument it has seen, for the app's lifetime.
Cached values are still dropped after gcTime, so this matters only for a
family with many thousands of arguments in one session, such as one keyed
by free-text search.
What's next
- Pause screens you can't see: stop hidden screens from refetching.
- Refetch on demand: pull to refresh, with a flag for the user's own refetch.
- Load more: page through a list from
onEndReached. - Focus and online signals: what the client does with each signal.