Skip to Content
Effetch Query
React

Read a query as an object

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

useQueryState reads a query as useQuery does, with the same options, and returns TanStack Query's shape: status, data, error, isFetching and refetch. Check status first; it narrows data and error:

The examples import todos.ts from Getting started.

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 fields are:

  • 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. A failed refetch keeps the last value as data.
  • error: the typed error, or undefined for a defect, which cause holds with the rest of the failure.
  • isFetching: true while a fetch runs, the first one or a refetch.
  • isPlaceholder: true while data is a placeholder, or the value keepPrevious keeps, rather than the query's own value (Show a placeholder). Use it to dim the data, or to turn off what needs the real value, such as editing it.
  • refetch: refetches the query, and resolves when it settles. It never rejects, and keeps its identity while the query stays the same.
  • result: the AsyncResult the fields come from, for AsyncResult.builder.

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.

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