Skip to content

Patterns

Loading, error, empty and no results

A view that loads data is always in one of four states: loading, an error with a way to try again, empty, or showing the data, and a filter that matches nothing is a different state from empty, with a different next step. In Meadowstack a screen loads through `useResource()` and renders through `<AsyncState>`, which draws exactly one state at a time, and `npm run check:states` fails any page that loads data without it.

By Bohuslav PoláčekChecked

templates/starter/src/modules/requests/pages/requests-list.tsx
<AsyncState resource={resource} onRetry={reload} loading={<LoadingState rows={8} label="Loading requests…" />}>
  {() => (
    <DataTable
      label="Purchase requests"
      columns={columns}
      data={rows}
      getRowId={(item) => item.id}
      getRowLabel={(item) => `${item.title}, ${item.number}`}
      enableSelection
      onSelectionChange={(ids) => setSelection({ key: selectionKey, ids })}
      selectionResetKey={selectionKey}
      onRowClick={(item) => openPeek(item.id)}
      activeRowId={peekId ?? undefined}
      onVisibleRowsChange={setVisibleIds}
      pageSize={25}
      isFiltered={isFiltered}
      onClearFilters={() => {
        setSearch("");
        setParams(
          (current) => {
            const next = new URLSearchParams(current);
            next.delete(keys.status);
            next.delete(keys.search);
            return next;
          },
          { replace: true },
        );
      }}
      emptyState={emptyState}
    />
  )}
</AsyncState>
The starter's purchase request list: AsyncState draws the skeleton, the error with its retry or the table, and the table tells empty from no results by isFiltered. Read from the repository when the site was built.

Open it in the live demo

The flow, step by step

What a person meets on the starter's purchase request list, and how to call up each state yourself:

  1. First load. A skeleton in the shape of the table, eight rows with the content's widths, so nothing jumps when the data arrives. Moving between pages in the demo shows a thin line at the top (RouteProgress), never a spinner over the whole page. Add ?latency=slow to the address to watch it for 1600ms.
  2. The server fails. The skeleton gives way to a sentence that says what failed and a "Try again" button, which loads the data again and nothing else. ?fail=1 makes every request fail.
  3. Nothing yet. An empty list says so and offers the first step: "No purchase requests yet", with New request for people who may create one. ?empty=1 empties every list.
  4. Nothing matches. A filter or a search that hides every row is a different state: it says nothing matches and offers to clear the filters. The filters live in the address, so Back undoes them too.
  5. A change. Approving a request changes its row at once and the toast offers Undo. When the api answers, the server's version (a new history entry, real ids) swaps in quietly, never through the loading state. When it refuses, the screen goes back to what is stored and the toast says why.
  6. Two quick changes. Approve, then Undo before the first answer lands. Each change takes a turn, only the newest answer is written, and the screen ends where the last click left it.

In the demo, the account menu's "For developers" part switches any screen, the task board included, to a slow network, failing requests or empty data.

The rules and where each one is enforced

What AI coding tools build instead

  • A fetch in useEffect with a loading flag. There is no error branch, so when the request fails the spinner turns for ever or the screen stays blank, and there is no cancellation, so an old answer overwrites a newer filter. This is the bug check:states exists for.
  • One "No data" for everything. An empty list and a filter that matches nothing say the same thing, and the person who filtered wonders where their records went.
  • A spinner in the middle of the page instead of a skeleton. It says nothing about what is coming, and the layout jumps when the data lands.
  • reload() after every save. The skeleton flashes over data the person was working with, and a picker they had opened meanwhile vanishes with it.
  • setData(valueBuiltBefore) after an await. The answer lands and puts back the screen as it was before the second click.
  • An optimistic change with no failure path. The screen keeps a change the server refused.
  • Tests for the success state only. The other three are where the bugs are.

Where to see it

Example apps that draw their states through AsyncState

Which example apps and modules render the kit's AsyncState, computed from the demo's code when the site was built.Read from the demo's source when the site was built: 9 of 9 example apps render <AsyncState>, in the modules named here as each app's sidebar names them.

The check that fails a page without states

scripts/check-states.mjs
const problems = [];
for (const file of scanDirs.flatMap((dir) => walk(dir))) {
  const source = readFileSync(file, "utf8");
  if (/\buseResource\s*\(/.test(source) && !/<AsyncState\b/.test(source) && !file.endsWith("use-resource.ts")) {
    problems.push(`${relative(root, file)}  loads data with useResource() but never renders <AsyncState>.`);
  }
}

if (problems.length) {
  for (const p of problems) console.error(p);
  console.error(`\nState check failed: ${problems.length} screen(s) without loading, error and empty states.`);
  process.exit(1);
}
console.log("State check passed.");
Part of npm run check: a screen that loads data with useResource() and never renders AsyncState stops the check, the blank screen when the api fails caught before anyone sees it.

Questions

What is the difference between an empty state and a no-results state?

Empty means nothing exists yet, and it offers the first step, such as New request. No results means a filter or a search hides everything, and it offers to clear the filters. One sentence for both leaves the person who filtered looking for records that are still there.

Should I show a spinner or a skeleton while data loads?

A skeleton in the shape of what is coming, on the first load and when a filter changes. A spinner says nothing about what is coming and the layout jumps when the data arrives. After a save, neither: the data swaps in quietly.

How do I see the error and empty states without breaking the server?

In the starter, add ?fail=1, ?empty=1 or ?latency=slow to the address; the flags are read once when the page loads. In the demo, the account menu's For developers part switches the same three on any screen.

What happens when two changes overlap on a slow network?

Each change takes a turn when it starts, and only the newest answer is written onto the screen. Approve, then Undo before the first answer lands, and the screen ends where the last click left it.

Does the check catch every screen without states?

It catches every screen that loads data through useResource() and never renders AsyncState. A screen that fetches some other way breaks a different rule of the framework: no data loading in an effect outside useResource.

Two ways

Build it yourself, or have it built

  • Build it yourself

    Build your app on the framework

    Get the codebase these apps stand on and build yours with Claude Code, Cursor or Codex.

    One payment, lifetime updates

    Get the framework
  • Have it built

    Have us build your app

    Tell us how your team works. We plan the app with you and build it on the same framework at a fixed price.

    After launch, Care keeps it improving, from €99 a month.

    from €1,990

    Describe your app