Sub topic 1
Every screen has more than one state
Prerequisites
This chapter assumes you know:
- Basic UI vocabulary: layout, components, and microcopy (the short text in buttons and messages).
- How a web or mobile app fetches data asynchronously, meaning the request starts, runs, and then returns a result or an error.
- Reading HTML and TypeScript at a basic level.
Every screen has more than one state
A screen that shows data can be in one of several states at a given moment: loading, empty, failed, or showing content. Each state needs its own design. If you design only the success state, users see blank areas or confusing text whenever the data is slow, missing, or broken.
Loading states and how waiting feels
Waiting feels shorter when the screen responds right away and shows the shape of what is coming. Show a visible loading state within about 100 ms of the user's action. Delay a spinner by about 300 ms so fast responses do not flash a spinner on screen.
Choosing a loading pattern
| Pattern | Best for | Avoid when |
|---|---|---|
| Spinner | Short, unknown waits (under about 2 s) | The wait is long or the layout will shift |
| Skeleton screen | Known layout, content loading in a list or card | The content shape varies a lot |
| Progress bar | Known total work, such as uploads | The total is unknown |
| Optimistic update | Low-risk actions likely to succeed, such as a like | Failure would cause costly or confusing rollback |
Why a screen is empty
A screen can be empty for different reasons, and each reason needs different text. A new user has no data yet, so the screen should invite a first action. A search returns nothing, so the screen should suggest changing the query. A filter hides everything, so the screen should offer a way to clear it. A failed request is an error, not an empty state.
type AsyncState<T> =
| { status: "loading" }
| { status: "error"; message: string; retryable: boolean }
| { status: "success"; data: T };
function renderList(state: AsyncState<string[]>): string {
switch (state.status) {
case "loading":
return "Loading projects…";
case "error":
return state.retryable ? `${state.message} Try again.` : state.message;
case "success":
return state.data.length === 0
? "No projects yet. Create your first project."
: state.data.join(", ");
}
}
console.log(renderList({ status: "success", data: [] }));> Note: keep the empty case distinct from the error case in your code, so the screen can never show "No projects" when the request actually failed.
Exercise
A search for "invoice" returns zero results because the user typed "invoce". Which empty-state cause is this, and what should the screen offer as its next action?
Show answer
It is a no-results empty state caused by the query. The screen should show the search text, suggest checking the spelling, and offer a way to clear the search.