Skip to Content
Effetch Query
Queries

Show a placeholder

Show placeholder data while a query loads.

A detail page can show what the list already has while the detail loads. placeholder returns Option.some of a value to show, or Option.none() for nothing. On api.query, it gets the call's params and query:

The examples import todos.ts from Getting started, and entities.ts from Entities.

todo-detail.ts
import {  } from "@monarcode/effetch-query";

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

export const  = .(, {
  : "1 minute",
  // A list's copy, if one is cached, until the todo's own fetch settles.
  : ({  }) => .(, (.)),
});

Entity.get reads the newest cached copy of the todo (Entities); any Effect that reads the cache works, such as Query.get of a list. Query.make takes placeholder as an Effect, and Query.family as a function of the member's argument. Keep it synchronous, reading the cache: an asynchronous one shows only once it's done, if the fetch hasn't settled first. If it dies, the defect is logged and nothing shows.

The placeholder shows as a waiting Success, so useSuspenseQuery doesn't suspend on it. Once the fetch settles, its value or its failure replaces it. It's never cached: Query.get, Query.update and Query.fetch don't see it, QueryClient.dehydrate doesn't send it, and a query reading this one through ctx.result waits for the real value. A server render doesn't show it.

To tell a placeholder from the query's own value, read isPlaceholder from useQueryState (Read a query as an object). It's true for the placeholder, and for the value keepPrevious keeps from the query shown before, until the query's own value or failure replaces it. A paused reader fetches nothing, so it keeps showing the placeholder, not fetching, until it resumes:

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

import {  } from "./todo-detail.js";

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

  if (. !== "success") {
    return null;
  }
  // Editing waits for the todo itself, not the list's copy.
  return (
    < ={..} ={.} />
  );
}