# React Native (https://effetch-query.pages.dev/docs/react/react-native)

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](https://effetch-query.pages.dev/docs/getting-started).

## Before you begin

You need React set up as [Installation](https://effetch-query.pages.dev/docs/installation#add-react)
shows, and NetInfo, which reports whether the device is online. Effetch
Query doesn't depend on it, so add it yourself:

```bash
pnpm add @react-native-community/netinfo
```

## Give the client React Native's signals

Pass `AppState` and NetInfo to `reactNativeSignals`, and its result to the
client's `signals` option:

```ts
import { QueryClient } from "@monarcode/effetch-query";
import { reactNativeSignals } from "@monarcode/effetch-query/react-native";
import NetInfo from "@react-native-community/netinfo";
import { AppState } from "react-native";

import { ApiClientLive } from "./api.js";

export const queryClient = QueryClient.make({
  layer: ApiClientLive,
  signals: reactNativeSignals({ AppState, NetInfo }),
});
```

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](https://effetch-query.pages.dev/docs/reference/limitations#fetching) 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-native` or NetInfo. `reactNativeSignals` takes anything that fits
  `AppStateLike` and `NetInfoLike`, 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](https://effetch-query.pages.dev/docs/react/pause) shows with
React Navigation's `useIsFocused`.

## Refresh and load more from FlatList

`FlatList` covers the two list gestures:

- For pull to refresh, pass `useRefetch`'s `isRefetching` and `refetch` to
  its `refreshing` and `onRefresh`: see
  [Refetch on demand](https://effetch-query.pages.dev/docs/react/refetch).
- For an infinite list, call `fetchNextPage` from its `onEndReached`: see
  [Load more](https://effetch-query.pages.dev/docs/queries/infinite#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](https://effetch-query.pages.dev/docs/client#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](https://effetch-query.pages.dev/docs/react/pause): stop hidden screens
  from refetching.
- [Refetch on demand](https://effetch-query.pages.dev/docs/react/refetch): pull to refresh, with a flag
  for the user's own refetch.
- [Load more](https://effetch-query.pages.dev/docs/queries/infinite#load-more): page through a list from
  `onEndReached`.
- [Focus and online signals](https://effetch-query.pages.dev/docs/client#focus-and-online-signals): what
  the client does with each signal.