CodingNic

Client State and Rendering

Show Loading and Retry States Clearly

Client State and Rendering 15 min read

Show Loading and Retry States Clearly

Open client/src/App.jsx and find AppBody().

At this point the context can tell us three important things about the initial read:

text
loading = true   → the request is still running
error            → the request failed
neither          → the data is ready to render

Use those states explicitly instead of rendering the normal pages immediately.

The loading branch can stay simple:

jsx
if (loading) {
  return (
    <p className="text-sm italic ledger-serif" style={{ color: TEXT_SECONDARY }}>
      Loading your ledger…
    </p>
  );
}

Then add the error branch before the normal <Routes>:

jsx
if (error) {
  return (
    <div className="text-sm">
      <p className="mb-3" style={{ color: NEGATIVE }}>
        Couldn't reach the server: {error}
      </p>
      <p className="text-xs mb-3" style={{ color: TEXT_SECONDARY }}>
        Make sure the API is running (`npm start` in the server folder, on port 4000).
      </p>
      <button
        onClick={refresh}
        className="text-xs px-2 py-1.5 border"
        style={{ borderColor: RULE, color: INK }}
      >
        Try again
      </button>
    </div>
  );
}

Why the order matters

The function checks loading first because an in-progress request should not display a stale error or an incomplete page.

It checks error second because a failed request should stop normal page rendering until the user retries successfully.

Only when neither condition is true does the component return the application’s routes.

The refresh function is passed directly to the button. Clicking Try again starts the same loading process used during the initial page load; we do not need a second implementation for retrying.

Test it

  1. Start both the client and server.
  2. Reload the page and watch for the loading message.
  3. Stop the API server.
  4. Reload the browser.
  5. Confirm the error message and Try again button appear.
  6. Restart the API server and click Try again.
  7. Confirm the normal Ledger returns.

Checkpoint

A slow request is shown as loading, an unavailable server produces a useful retry screen, and a successful retry returns to the normal application.