Introduction
In modern web development, most applications depend on external data sources such as APIs. Whether you're building a dashboard, blog, e-commerce site, or admin panel, fetching data from an API is a core skill every React developer must understand.
React provides powerful tools like useState and useEffect hooks that make it easy to fetch and display API data in a clean and efficient way. In this article, we will learn step-by-step how to fetch API data in React, store it in state, and display it on the UI.
This guide is written in simple and practical language, making it easy for beginners to understand while still being useful for experienced developers.
What is an API?
An API (Application Programming Interface) allows different software systems to communicate with each other. In simple terms, an API lets your React application request data from a server and receive it in a structured format, usually JSON.
For example:
Fetching user data from a server
Getting product details for an e-commerce app
Loading blog posts dynamically
Understanding useState in React
The useState hook is used to store and manage data inside a React component.
When working with APIs, we use useState to:
Store fetched data
Track loading state
Handle errors
Example:
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
Explanation:
data → stores API response
loading → tracks whether data is being fetched
error → stores any error that occurs
Understanding useEffect in React
The useEffect hook is used to perform side effects in React components. Fetching data from an API is considered a side effect.
useEffect runs when the component is rendered.
Example:
useEffect(() => {
// API call here
}, []);
The empty dependency array [] ensures the API is called only once when the component loads.
Step-by-Step: Fetch API Data in React
Let’s build a simple example where we fetch user data from a public API and display it.
Step 1: Create a React Component
import React, { useEffect, useState } from "react";
function Users() {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
return <div></div>;
}
export default Users;
Step 2: Fetch Data Using useEffect
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/users")
.then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch data");
}
return response.json();
})
.then((data) => {
setUsers(data);
setLoading(false);
})
.catch((error) => {
setError(error.message);
setLoading(false);
});
}, []);
Explanation:

Comments
Join the conversation! Your thoughts help the community grow.