Skip to Content
Effetch Query
React

Read a query as an object

Read a query's data, error and loading flags as fields.

If you'd rather check fields than match on an AsyncResult, as you would with TanStack Query's useQuery, read the query with useQueryState. It fetches and takes options as useQuery does, select, keepPrevious and paused included, and returns status, data, error, isFetching, refetch and a few more fields. This page shows how to read them, where they differ from TanStack Query's, and the hooks that return the same object for Suspense, infinite queries and lists of queries.

The examples import todos.ts from Getting started.

Check the status, then read the fields

status narrows data and error, so check it first:

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

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

export function ({  }: { readonly : number }) {
  const  = (({ : {  } }));

  if (. === "pending") {
    return <>Loading…</>;
  }
  if (. === "failure") {
    return (
      < ={() => void .()}>
        Couldn't load the todo: try again
      </>
    );
  }
  return (
    < ={{ : . ? 0.5 : 1 }}>{..}</>
  );
}

The object's type is UseQueryStateResult, a union of three shapes told apart by status. The object keeps its identity while nothing in it changes.

The fields

useQueryState returns these fields:

  • status: "pending", "success" or "failure", with isPending, isSuccess and isFailure to match. A query is pending before its first fetch settles, after a clear, and when it's null.
  • data: the value, or its selection with select. A failed refetch keeps the last value as data.
  • error: the typed error. It's undefined when the query died with a defect or was interrupted.
  • cause: the failure's full Cause, which holds the error, the defect or the interruption.
  • isFetching: true while a fetch runs, the first one or a refetch.
  • isPlaceholder: true while data isn't the query's own value yet: see Tell a placeholder from the real value.
  • refetch: refetches the query, and resolves when the refetch settles. It never rejects, and keeps its identity while the query stays the same.
  • result: the AsyncResult the fields come from.

error is a union of every error the query can fail with. To handle each by its _tag, match on state.result, or on useQuery's result: see Read a query.

Differences from TanStack Query

The fields are close to TanStack Query's, not the same. Coming from it, these are the differences:

  • status is "failure", not "error", and its flag is isFailure, not isError.
  • isPlaceholder replaces isPlaceholderData.
  • refetch resolves with nothing, not with the new result, and never rejects: a failure shows in the fields.
  • error is undefined for a defect or an interruption. Read cause for those.
  • There's no isLoading, isRefetching or isStale. For a refetch you started yourself, such as pull to refresh, use useRefetch's isRefetching: see Refetch on demand.

Tell a placeholder from the real value

Some values a component shows aren't the query's own yet: a placeholder, such as the list's copy of a todo, or the previous todo that keepPrevious keeps while the next one loads. isPlaceholder is true for both. Use it to dim the data, or to turn off what needs the real value, such as editing it:

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

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

export function ({  }: { readonly : number }) {
  const  = (({ : {  } }), {
    : true,
  });

  if (. !== "success") {
    return null;
  }
  // Editing waits for this todo, not the one kept from before.
  return (
    < ={..} ={.} />
  );
}

isPlaceholder stays true until the query's own value or failure replaces the placeholder, and isFetching is true meanwhile. A failure's data is always the query's own last value, so isPlaceholder is false there.

A paused reader fetches nothing, so it keeps showing a placeholder, with isPlaceholder true and isFetching false, until it resumes.

Suspense treats a placeholder as a value. In the browser, useSuspenseQuery doesn't suspend on one: it returns it as a waiting Success. A server render doesn't show a placeholder, so there, and while the page hydrates, useSuspenseQuery suspends until the query's own value arrives.

Read with Suspense

useSuspenseQueryState reads a query as useSuspenseQuery does, and returns the same fields. It suspends until the query has a value, so data is always there:

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

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

export function ({  }: { readonly : number }) {
  const  = (({ : {  } }));

  return (
    <>
      {..}
      {. ? (
        < ={() => void .()}>Couldn't refresh</>
      ) : null}
    </>
  );
}

status is "success" or "failure", never "pending". A failure with no value before it goes to the error boundary, as with useSuspenseQuery. A refetch that fails after a success doesn't: useSuspenseQuery keeps showing the success, and useSuspenseQueryState shows the failure, with the earlier value as data and the typed error beside it. Its only option is select.

Read pages

useInfiniteQueryState and useSuspenseInfiniteQueryState read an infinite query as useInfiniteQuery and useSuspenseInfiniteQuery do. They return the fields, with data.pages for the pages, beside the page controls:

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

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

export function () {
  const  = (({ : { : "web" } }));

  if (. === "pending") {
    return <>Loading…</>;
  }
  return (
    <>
      <>
        {.?.
          .(() => .)
          .(() => (
            < ={.}>{.}</>
          ))}
      </>
      {. ? (
        <
          ={.}
          ={() => void .()}
        >
          Load more
        </>
      ) : null}
    </>
  );
}

A page that fails makes status "failure", with the pages loaded before it still in data. A failure on the first page has no pages, so data is undefined there. useSuspenseInfiniteQueryState throws that one to the error boundary instead, so its data is always there.

Read several queries

useQueriesState reads a list of queries as useQueries does, and returns an object for each, in the same order:

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

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

export function ({
  ,
}: {
  readonly : <number>;
}) {
  const  = (.(() => ({ : {  } })));

  return (
    <>
      {.((, ) => (
        < ={[]}>
          {. === "success" ? .. : "Loading…"}
        </>
      ))}
    </>
  );
}

Each object keeps its identity while its query's result does, and its refetch while the query stays in the list. A null entry is pending, and its refetch resolves at once. It takes paused, as useQueries does.

What's next

On this page