A table loads. The user starts reading. A background request runs, and the entire table disappears behind a spinner.
Nothing necessarily failed. The interface just treated “a request is running” as “there is nothing useful to show.”
That is a contract problem as much as a rendering problem.
When we design an API, we usually agree on the response shape. We should also agree on what the interface can truthfully say while that response is missing, outdated, incomplete, or no longer accessible.
I don't mean putting a showSpinner field in every endpoint. I mean making the boundary between the service, the cache, and the screen explicit before the happy path becomes the whole implementation.
Start with two questions
For any data-driven screen, ask:
- What information can we safely show right now?
- What is happening to the request that might change it?
Those are separate questions. A request can be running while useful data remains visible. A request can fail while a previous result still exists. And a screen can have no data without a request currently running, because it is waiting for a selection or a connection.
TanStack Query v5 reflects this distinction with separate data and fetch states. isFetching includes background requests; a pending query does not necessarily mean a request is currently in flight. That distinction is worth preserving in the UI rather than flattening into one loading boolean. Queries documentation
Write the behaviour before the component
Consider a hypothetical project dashboard. Here is the contract I would want to settle with product and API owners:
Situation | What the user sees | What the system must know |
|---|---|---|
First request, no snapshot | Stable placeholder and a loading message | Whether a request is running or paused |
Refreshing the same project | Existing content with a small updating indicator | Which project the snapshot belongs to |
Refresh failed | Existing content plus a refresh failure message, if retention is safe | Whether the old snapshot remains usable |
Successful response with no rows | An empty state that matches the active filters | Whether the result is complete |
Optional panel unavailable | Available panels plus a local explanation | Which section failed and whether it can retry |
Sign-in expired or access denied | The appropriate authentication or access state | Whether previously loaded content must be hidden |
The last row matters. “Keep the old data on error” is not a universal rule. Cached project information should not stay on screen after the application learns that access has been revoked.
Likewise, permission to view an old snapshot does not imply permission to perform an action against it. The server still owns authorization and validates the current state when an action arrives.
Empty is a result, not a fallback
This expression looks harmless:
It can be fine inside code that has already established a successful response. Used as the entire state model, it makes “not loaded” indistinguishable from “loaded and empty.”
That is how a user sees “Create your first project” while their existing projects are still loading.
A filtered empty result also deserves different copy from a genuinely new account. “No projects match these filters” suggests changing the query. “No projects yet” suggests creating one. Neither should stand in for a failed request.
Partial data needs the same care. If the activity service is unavailable, an empty activity array should not quietly mean “nothing happened.” Give that section an explicit unavailable state. An API envelope might model it like this:
This is an illustrative application contract, not a framework requirement. Its value is that the frontend no longer has to guess whether an empty array is trustworthy.
Keep the query identity attached to the data
Suppose someone switches from project A to project B. Retaining A's results under B's heading is worse than showing a loading state.
Include the variables that identify the requested result in the query key. TanStack Query's guidance explicitly connects changing query-function dependencies with cache identity. Query keys documentation
For a project list, that might mean the workspace and filters. For an account-specific view, it also means deliberately handling account changes and cache clearing. A key is not an authorization boundary.
Previous results can sometimes remain visible during a filter change, but they need to be clearly identified as previous results. Do not quietly present them as the answer to the new question.
Give rendering an explicit order
Here is a shortened React example for a read-only list using TanStack Query v5. It assumes the query is enabled, its key identifies the current project, and data only contains a snapshot for that identity. The named components and error classifier are application code, not library APIs.
The important part is the decision order: check access, distinguish missing data from retained data, then describe the request without unnecessarily removing the content.
This is not a complete authentication implementation. The application still needs a central response to session changes, appropriate cache invalidation, and an explicit policy for retaining data after other kinds of failure. If a query can be disabled until a user chooses something, give that waiting state its own branch too.
Retry is part of the experience
A retry button should represent a plausible recovery path. An unchanged forbidden request is not one. HTTP 403 indicates that the server refuses the request; repeating it unchanged is expected to fail. HTTP 403 documentation
For reads, define which failures may retry automatically, how many attempts are reasonable, and when the user gets control. TanStack Query supports a retry predicate based on the failure and attempt count, so that policy does not have to be scattered across components. Retry documentation
Keep write retries separate. A list refresh and a payment submission have different consequences. Do not carry a read-oriented retry policy into mutations without understanding duplicate execution and the server's guarantees.
Freshness also needs a product definition. A cache timestamp tells you when the client received a result, not necessarily when the underlying information was last updated. If the interface promises “as of 10:30,” agree on what that timestamp actually represents.
Test transitions, not just screenshots
A useful review exercises sequences:
- First load succeeds with no rows. Does the empty state appear only after success?
- A populated list refreshes. Does useful content remain visible?
- That refresh fails. Can the user understand what they are looking at and recover?
- The user changes projects while a request is pending. Can old data appear under the new heading?
- A session expires after a successful load. Does cached content follow the access policy?
- One optional panel fails. Does the rest of the page remain usable without implying the missing panel is empty?
Include keyboard and screen-reader behaviour. A visual spinner alone does not communicate status to everyone. W3C explains that qualifying status messages should be programmatically identifiable without taking focus. Use a suitable status region for concise updates, and test that repeated background activity does not produce excessive announcements. Status messages guidance
The contract is the decision
Before building the next data screen, write down the data identity, the meaning of empty and partial results, the retention policy, the access-failure behaviour, and the recovery path.
Then use those decisions to drive the API shape, cache configuration, component states, and tests.
A loading indicator is easy to add. Making sure the screen tells the truth while the system changes underneath it takes a little more design.
