In Single-Page Applications, data does not live inside the HTML bundle. Instead, client-side React communicates asynchronously with backend REST APIs over HTTP using JSON serialization.
A production-grade data fetching workflow must manage:
In React, useEffect cannot accept an async function directly. The standard pattern defines an async function inside the effect and executes it:
fetch() response.ok TrapA critical misunderstanding among JavaScript engineers: The native window.fetch() Promise only rejects on complete network failures (e.g. offline, DNS failure).
If a backend server returns HTTP 404 Not Found or HTTP 500 Internal Server Error, fetch() still resolves successfully!
You must explicitly check if (!response.ok) to verify that the HTTP status code is in the 200–299 range before attempting to parse response.json().
try...catch block around fetch() catch an HTTP 404 or 500 response automatically?async keyword placed on an internal helper function rather than directly on useEffect (e.g. useEffect(async () => ...) is invalid)?let isMounted = true boolean flag in the effect?setLoading(false) inside a finally block?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Combining useReducer with Context API | Handling HTTP POST, PUT, and DELETE Mutations |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.