Skip to Content
Effetch Query
React

Pause screens you can't see

Stop hidden screens from refetching.

Some screens stay mounted while you can't see them: the tabs you aren't on, the screens a stack navigator keeps under the one in front, a panel that's hidden rather than removed. Their components still read their queries, so the queries stay in use: they refetch on every invalidation, and on focus when they're stale, and the components re-render out of sight. Pass paused while the screen is hidden.

The examples import todos.ts from Getting started.

Pause a hidden screen

Here a React Navigation screen pauses while it's not focused. The example declares useIsFocused, which your app imports from @react-navigation/native:

import * as  from "effect/reactivity/AsyncResult";

import {  } from "@monarcode/effetch-query/react";

import {  } from "./todos.js";

// From @react-navigation/native.
declare function (): boolean;

export function () {
  const  = ();
  const  = ((), { : ! });

  return <>{.(, () => []).} todos</>;
}

A query is in use while something subscribes to it, such as a mounted component that reads it: see Queries in use. A paused reader doesn't subscribe, so it doesn't count. It keeps showing what it had, and an invalidation, focus, reconnect or refetchInterval doesn't refetch the query for it, nor re-render the component.

When the screen comes back, the query refetches if it's stale or failed, as the mount policy does when a component mounts, unless another reader fetched it meanwhile and it's still fresh.

What a paused reader shows

A paused reader shows what it had, with a few rules:

  • What it shows isn't waiting, since nothing fetches for it. A placeholder stays a placeholder until it resumes.
  • Paused from its first render, a reader shows what the cache has, without fetching.
  • A query whose readers are all paused isn't in use, so it's dropped after gcTime like any other, though the paused readers still show what they had.
  • A clear replaces what they show with what the cache has, usually nothing, so the screen shows Initial.
  • useQueryState's refetch resolves at once for a query that only paused readers use, since a paused reader isn't a use.

Which hooks take paused

useQuery, useQueryState, useQueries and useInfiniteQuery take paused. useInfiniteQuery's page functions still fetch pages when called: see Load more. useSuspenseQuery doesn't take it.

What's next

On this page