Full Stack JS

Chapter 9
0%

Chapter 9: React API & Forms

Learn how to connect your React app to the outside world — fetching data, sending forms, and building real interactive apps.

What is an API?
HTTP Methods
Axios Setup
CRUD Operations
Forms in React
Quiz

What is an API?

Imagine you're at a restaurant. You (the customer) want food from the kitchen, but you can't just walk in and cook it yourself. Instead, a waiter takes your order, brings it to the kitchen, and delivers the food back to you. An API is that waiter — it's the middleman between your React app (the customer) and the server/database (the kitchen).

API stands for Application Programming Interface. Here's what that means in practice:

What Does an API Look Like?

An API is just a URL (called an endpoint) that returns data instead of a web page:

API Endpoints — Examples
JSON (JavaScript Object Notation) is the "language" APIs speak. It looks exactly like JavaScript objects — curly braces, key-value pairs, arrays. That's why JavaScript/React is so great for working with APIs!

HTTP Methods

Back to our restaurant analogy: there are different types of requests you can make. You can order food (GET), place a new order (POST), change your order (PUT), or cancel your order (DELETE). HTTP methods work the same way!
MethodWhat It DoesRestaurant AnalogyExample
GETRead/fetch dataLooking at the menuGet list of users
POSTCreate new dataPlacing a new orderCreate a new user
PUTUpdate existing dataChanging your orderUpdate user's email
DELETERemove dataCanceling your orderDelete a user

HTTP Status Codes

When the API responds, it includes a status code — a number that tells you what happened:

CodeMeaningWhen You See It
200OK - Success!Everything worked fine
201CreatedNew item was successfully created
400Bad RequestYou sent invalid data
404Not FoundThe endpoint/resource doesn't exist
500Server ErrorSomething broke on the server

Axios Setup

Axios is a popular library for making API calls in React. It's simpler and more powerful than the built-in fetch API.

Step 1: Install Axios

Terminal — Install Axios

Step 2: Create an Axios Instance

Instead of typing the full URL every time, create a reusable instance with a base URL:

src/api/axios.js — Axios Instance
Why create an instance? If your backend URL changes, you only need to update it in ONE place. Without an instance, you'd have to update every single API call in your app!

CRUD Operations

CRUD stands for Create, Read, Update, Delete — the four basic operations for any data-driven app.

GET — Fetching Data

React — GET Request (Fetch Users)
Always handle 3 states: loading, error, and success. Without these, your app will either show a blank screen while data loads, or crash silently when something goes wrong.

POST — Creating Data

React — POST Request (Create User)

PUT — Updating Data

React — PUT Request (Update User)

DELETE — Removing Data

React — DELETE Request (Delete User)

Forms in React

In plain HTML, forms just submit to a URL. In React, forms are controlled — React keeps track of every keystroke, every selection, every change. Think of it like a smart assistant who writes down every letter you type, so you always know exactly what the form says at any moment.

Controlled Form Pattern

React — Controlled Form with API Call
The [name] trick: Notice how handleChange uses [name]: value. The name attribute on each input matches the key in formData. This lets ONE handler work for ALL fields instead of writing a separate handler for each!
Always call e.preventDefault() in your submit handler! Without it, the browser will refresh the page and you'll lose all your React state.

📝 Chapter 9 Quiz

1. What is an API?

A database that stores data
A middleman that lets your app communicate with a server
A React component for forms
A CSS framework for styling

2. Which HTTP method is used to CREATE new data?

GET
PUT
POST
DELETE

3. In Axios, where is the actual data from the server?

response.data
response.body
response.json
response.content

4. Why do we call e.preventDefault() in form submit handlers?

To make the form faster
To validate the form inputs
To send data to the server
To stop the browser from refreshing the page

5. What does HTTP status code 404 mean?

Success
Not Found
Server Error
Bad Request
← Chapter 8: React Router Chapter 10: React Full Project →