STAGE 02 / LESSON 05
Fetching data and handling forms
A useful interface handles loading, success, empty and error states.
THE IDEA
Understand it.
An API can return a non-success HTTP response even when the network request itself succeeds. Check the response status, handle errors, and validate the shape of data before using it.
For forms, label inputs, validate on the server, and give a useful error message when a request fails. Be explicit about caching and revalidation requirements; framework defaults can change between versions.
SEE IT IN CONTEXT
Fetch tasks in the browser
async function loadTasks() {
const response = await fetch("/api/tasks");
if (!response.ok) {
throw new Error("Could not load tasks");
}
return response.json(); // validate before trusting its shape
}fetch does not automatically throw for a 404 or 500 response, so the code checks response.ok. A relative URL here assumes this runs in the browser.
LEARN FROM REAL WEBSITES
See it in the real world.
These simplified structures come from public sources. Company systems change, and the sources may describe only one project or an earlier design.
Spotify
Tutorial checked November 2026Fetch a user's profile
Spotify's tutorial walks through authorization, requesting profile data and displaying it in a web app.
- Authorize user
- Request profile
- Receive data
- Render profile
What to learn: A useful interface must handle the boundary between remote data and the page; this is a public tutorial.
Read the source: Spotify profile web app tutorialPayPal
Tutorial checked November 2026A checkout integration
PayPal publishes a Next.js checkout guide showing how an application can integrate payment controls and server-side actions.
- Checkout page
- PayPal integration
- Server-side order handling
What to learn: The guide illustrates an integration a developer can build; it is not evidence that the PayPal website itself uses this stack.
Read the source: PayPal Next.js checkout guideTRY IT YOURSELF
Practice.
What should a user see when the server returns an empty task array?
Show one possible answer
An understandable empty state, such as 'No tasks yet', rather than a blank screen or an error intended for a failed request.
GO DEEPER