CHAPTER 18
Beginner
API Calls and Fetch Requests
Updated: May 16, 2026
7 min read
# CHAPTER 18
API Calls and Fetch Requests
1. Introduction
A mobile app without an internet connection is just a calculator. Modern applications rely on external servers to provide dynamic content: live weather updates, social media feeds, banking records, and e-commerce catalogs. To get this data, your app must communicate with a web server using an Application Programming Interface (API). In this chapter, we will master API Calls and Fetch Requests. We will utilize JavaScript's built-infetch API, handle asynchronous network delays using async/await, manage UI loading states, and successfully execute GET and POST requests.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the lifecycle of an HTTP request.
-
Execute an HTTP GET request using the
fetchAPI.
- Parse server responses from raw Strings into JSON objects.
-
Manage
isLoadinganderrorstates in the UI.
- Execute an HTTP POST request to send data to a server.
3. The fetch API and Asynchronous Logic
When an app asks a server across the world for data, it might take 2 seconds to reply. If the app freezes for 2 seconds while waiting, the OS will crash it.
We must use Asynchronous Programming (async/await) to tell React: *"Go fetch the data in the background, but let the user keep scrolling the app in the meantime."*
4. Executing a GET Request (Fetching Data)
A GET request is used to retrieve data (like a list of users). We will use the free JSONPlaceholder API for testing.
javascript
5. Integrating APIs with React State and useEffect
You rarely want to press a button to load data. Usually, you want the data to fetch automatically the millisecond the screen opens!
To do this, we put our fetch call inside a useEffect hook with an empty dependency array [] (meaning: run this exactly once when the component mounts).
We also need three pieces of State: The Data, the Loading indicator, and Error messages.
javascript
6. Executing a POST Request (Sending Data)
A POST request is used to create or send data (like logging in or submitting a form). To use POST, you must add an options object to thefetch call, specifying the method, headers, and the stringified body data!
javascript
7. Visual Learning: The API Lifecycle
txt
8. Common Mistakes
-
Forgetting
response.json(): A common beginner error is doingconst data = await fetch(url);and then trying toconsole.log(data.name). Thefetchcall returns a Response *Object* containing headers and status codes, not the actual JSON body. You MUST perform a second asynchronous step:await data.json()to parse the body text into usable JavaScript objects!
9. Best Practices
-
The
finallyBlock: Notice in Section 5 we putsetIsLoading(false)inside afinallyblock instead of at the end of thetryblock. Thefinallyblock executes *no matter what happens*. If the internet fails and thecatchblock runs, the loading spinner will still be turned off, ensuring the user isn't trapped staring at an infinite spinning wheel.
10. Practice Exercises
- 1. What built-in React hook is used to automatically trigger an API fetch call exactly once when a screen first opens?
-
2.
What JavaScript function must be used to convert a JavaScript Object into a raw string before sending it inside the
bodyof a POST request?
11. MCQs with Answers
Question 1
When making an HTTP POST request using the Fetch API, why is it mandatory to include 'Content-Type': 'application/json' in the headers?
Question 2
In a standard data-fetching component, what is the architectural purpose of implementing an isLoading boolean state variable?
12. Interview Questions
-
Q: Walk me through the complete lifecycle of a component fetching data on mount, focusing on the interplay between
useEffect,fetch, and the sequential updates toisLoadingand data states.
-
Q: Explain the necessity of the
JSON.stringify()method when executing a POST request body, and its counterpartresponse.json()when receiving data. Why can't raw JavaScript objects traverse the network?
-
Q: Describe how you would gracefully handle a
500 Internal Server Errorresponse from afetchcall, specifically regarding updating the UI to inform the user.
13. FAQs
Q: Myfetch call works on iOS but fails on the Android Emulator when I try to connect to my local computer's Node.js server (http://localhost:3000). Why?
A: localhost on the Android emulator points to the emulator itself, not your computer! You must replace localhost with the special Android alias 10.0.2.2 (e.g., http://10.0.2.2:3000) or use your computer's actual physical IP address.
14. Summary
In Chapter 18, our application connected to the world. We embraced Asynchronous Programming, utilizingasync/await to process network delays without freezing the UI. We mastered the built-in fetch API, executing GET requests to retrieve external data and POST requests to send form data. We orchestrated the complex lifecycle of network requests using useEffect, ensuring users are greeted with professional ActivityIndicators while data loads securely into the application's state.
15. Next Chapter Recommendation
Whilefetch is great, it requires a lot of repetitive boilerplate code (like manually parsing JSON every time). The enterprise industry uses a more powerful library. Proceed to Chapter 19: Axios and API Integration.