Fetching a list in React is easy to start: call fetch, parse JSON, and render the result. The useful version also explains what is happening while the request runs, what to do when it fails, and what an empty response means.
This example uses a small client-side component and an endpoint at /api/users that returns an array of objects with id and name. If your framework already provides a data-loading API, start there. React's own documentation explains why fetching directly in an Effect can introduce extra requests and missed caching opportunities.
The four outcomes to show
A request can be loading, fail, succeed with no items, or succeed with items. Keep these outcomes separate. An empty array is a valid response; it is not a network error.
The status should drive the message on screen. Use a loading message while waiting, an error with a Retry action when a request fails, and a clear empty message when the returned array has no users.
A complete component
The request runs when the component mounts and again when Retry changes the attempt number. The cleanup aborts an old request when the component unmounts or a newer attempt starts, so an outdated response cannot replace newer data.
Fetch does not reject just because the server returns a 404 or 500 response. Check response.ok before parsing the body. The shape check is small but useful: the component expects a list, not an arbitrary JSON value.
import { useEffect, useState } from "react";
export function UsersList() {
const [users, setUsers] = useState([]);
const [status, setStatus] = useState("loading");
const [attempt, setAttempt] = useState(0);
useEffect(() => {
const controller = new AbortController();
async function load() {
setStatus("loading");
try {
const response = await fetch("/api/users", { signal: controller.signal });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
if (!Array.isArray(data)) throw new Error("Expected a list");
if (controller.signal.aborted) return;
setUsers(data);
setStatus("success");
} catch (error) {
if (!controller.signal.aborted) setStatus("error");
}
}
load();
return () => controller.abort();
}, [attempt]);
if (status === "loading") return <p role="status">Loading users...</p>;
if (status === "error") return (
<div role="alert">
<p>Could not load users.</p>
<button onClick={() => setAttempt((n) => n + 1)}>Retry</button>
</div>
);
if (users.length === 0) return <p>No users found.</p>;
return <ul>{users.map((user) => <li key={user.id}>{user.name}</li>)}</ul>;
}How to check it
Try a successful array, an empty array, a 500 response, and a disconnected network. The 500 response should show the error state, not an empty list. Retry should start a fresh request.
Also switch away from the screen before a slow request finishes. The cleanup should prevent that old result from updating the component. If this screen needs caching, preloading, or data shared across routes, use the framework's loader or a data-fetching library rather than growing this example into a custom cache.