Skip to Content
Effetch Query
React

Refetch on demand

Refetch a query from an event, such as a button or pull-to-refresh.

A query's waiting is true for background refetches too, which shouldn't show a spinner. useRefetch refetches on demand, with a flag for its own refetch only:

The examples import api.ts and todos.ts from Getting started.

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

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

export function () {
  const { ,  } = ();
  return (
    < ={} ={() => void ()}>
      { ? "Refreshing…" : "Refresh"}
    </>
  );
}
// React Native: <FlatList refreshing={isRefetching} onRefresh={refetch} … />

useRefetch takes a target (Targets) and refetches its queries in use, as Query.refetch does. It returns a UseRefetchResult:

  • refetch resolves when the queries have settled, and never rejects. It keeps its identity across renders, and uses the target of the latest render, so a key array written inline is fine.
  • isRefetching is true only while one of this component's refetches is in progress. Focus, reconnect and polling don't set it.

That makes it the hook for pull to refresh: in React Native, pass isRefetching and refetch to FlatList's refreshing and onRefresh.

A screen that shows queries from several features, each with its own key, refreshes them together with Query.group. Define the group once: it's a target like any other, so a mutation can invalidate it too. Here the home screen refreshes the todos and the profile together:

import * as  from "effect/Schema";

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

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

const  = .(
  .("GET /me", { : .({ : . }) }),
);

// Everything the home screen shows. The spinner stops once all of it has settled.
export const  = .(, );

export const  = .(
  .("POST /todos/archive", { : .() }),
  { : [] },
);

export function () {
  const { ,  } = ();
  return (
    < ={} ={() => void ()}>
      Refresh
    </>
  );
}

Pass the group, not an array: useRefetch([todos, profile]) compiles, because an array is a key, but a key that holds queries would refetch nothing, so useRefetch throws a Query.KeyHoldsTargetError as it renders.