Concept
Fetching in an effect means handling loading, error and success states, ignoring responses that arrive after the component changed its mind, and cancelling when it unmounts. A small hook or a data library hides this once.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Fetching data in components doing its job.
useEffect(() => {
const controller = new AbortController();
setState({ status: "loading" });
fetch("/api/users/" + id, { signal: controller.signal })
.then((r) => r.ok ? r.json() : Promise.reject(new Error("HTTP " + r.status)))
.then((user) => setState({ status: "ready", user }))
.catch((error) => {
if (error.name !== "AbortError") setState({ status: "error", error });
});
return () => controller.abort();
}, [id]);Ignoring race conditions. When id changes quickly, an earlier slower response can land last and overwrite the newer data. Abort or flag the stale request in cleanup.
Your roadmap puts these before Fetching data in components, unless you already know them.
Once a session shows you understand Fetching data in components, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on Fetching data in components.