Skip to Content
Effetch Query
Queries

Show a placeholder

Show placeholder data while a query loads.

A todo's detail page can show what the list already has, its title and whether it's done, while the full todo loads. A placeholder is that value: something to show until the query's own fetch settles. This page shows how to give a query one, and what a placeholder is and isn't.

The example imports todos.ts from Getting started, and entities.ts from Entities.

Give a query a placeholder

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. Here is todo from todos.ts with one. In your app, it replaces the todo that todos.ts defines:

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, from any cached query whose schema holds todos, such as the list. Any Effect that reads the cache works, such as Query.get of a list. On Query.make, placeholder is an Effect, and on Query.family a function of the member's argument.

Keep it synchronous, reading the cache. An asynchronous one shows only after it's done, if the fetch hasn't settled first, and a suspense reader that's already suspended doesn't get it: it waits for the fetch. If a placeholder dies, the defect is logged and nothing shows.

What a placeholder is

A placeholder is shown, never cached. It shows as a waiting Success, so a component renders it as it would the value, and useSuspenseQuery doesn't suspend on it. When the fetch settles, its value or its failure replaces the placeholder.

Since it isn't cached:

  • Query.get, Query.update and Query.fetch don't see it;
  • QueryClient.dehydrate doesn't send it, and a server render doesn't show it;
  • a query that reads this one through ctx.result waits for the real value.

After Query.reset, the placeholder shows again until the next fetch settles.

Tell a placeholder from a value

Sometimes a component needs to know which one it shows: an edit form shouldn't open on the list's copy of a todo. useQueryState returns an isPlaceholder flag for this. Read a query as an object shows it, and how it works with keepPrevious and paused.

What's next

On this page