React Fetch API using useEffect Hook (Step-by-Step Example)
๐ INTRODUCTION
In modern web applications, data is usually fetched from external APIs. React provides the useEffect hook to handle such side effects.
In this program, we will:
- Fetch data from an API
- Store it using useState
- Display it dynamically
๐ This is a must-know concept for projects and placements.
๐ PROBLEM STATEMENT
๐ Write a program to fetch and display data from an API using the useEffect hook in React.
๐ก CONCEPTS USED
- useEffect Hook
- useState Hook
- Fetch API
- Asynchronous Programming
๐ง LOGIC EXPLANATION
- Create state to store data
- Use
useEffect()to call API - Fetch data using
fetch() - Store response in state
- Display data using
map()
๐ป PROGRAM CODE
Open src/App.js:
import { useEffect, useState } from "react";
function App() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/users")
.then(response => response.json())
.then(data => setUsers(data));
}, []);
return (
<div>
<h1>User List</h1>
<ul>
{users.map(user => (
<li key={user.id}>
{user.name} - {user.email}
</li>
))}
</ul>
</div>
);
}
export default App;
๐ฅ๏ธ OUTPUT
User List
Leanne Graham - leanne@example.com
Ervin Howell - ervin@example.com
...
๐ STEP-BY-STEP EXPLANATION
useState([])โ stores API datauseEffect(() => {}, [])โ runs once on loadfetch()โ retrieves data from APIsetUsers(data)โ updates statemap()โ displays list
โก USING ASYNC/AWAIT (MODERN WAY)
useEffect(() => {
const fetchData = async () => {
const response = await fetch("https://jsonplaceholder.typicode.com/users");
const data = await response.json();
setUsers(data);
};
fetchData();
}, []);
โ ๏ธ IMPORTANT POINT
๐ Empty dependency array [] ensures:
- API is called only once
- Prevents infinite loops
๐ฏ WHY useEffect IS IMPORTANT
- Handles side effects (API calls, timers, etc.)
- Runs after component renders
- Core concept for React projects
๐ ADVANCED FEATURES
1. Loading State
const [loading, setLoading] = useState(true);
2. Error Handling
.catch(error => console.log(error));
3. Display Data in Cards
๐ Combine with Product/User components
๐ REAL-WORLD USE CASE
- Fetching user data
- Displaying products
- Dashboard APIs
- Live data applications
โ VIVA QUESTIONS (IMPORTANT)
Q1. What is useEffect?
๐ A React hook used to handle side effects.
Q2. What is Fetch API?
๐ A method to request data from servers.
Q3. Why use empty dependency array?
๐ To run effect only once.
Q4. What is async/await?
๐ A cleaner way to handle asynchronous code.
Q5. What happens if dependency array is missing?
๐ Effect runs on every render.
๐ RELATED POSTS
๐ MCA 168 Full Stack Development Lab Programs List
๐ React User Profiles โ Program 13
๐ React List Rendering โ Program 7
๐ React Props โ Program 10
๐ CONCLUSION
Fetching data using useEffect is one of the most essential skills in React.
๐ Master this to build real-world applications like dashboards, e-commerce sites, and APIs.
Further Reading
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
