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

  1. Create state to store data
  2. Use useEffect() to call API
  3. Fetch data using fetch()
  4. Store response in state
  5. 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 data
  • useEffect(() => {}, []) โ†’ runs once on load
  • fetch() โ†’ retrieves data from API
  • setUsers(data) โ†’ updates state
  • map() โ†’ 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

JUnit Tutorial

How to Master Full Stack Development?

Spring Framework Practice Problems and Their Solutions

30 MCQs on JUnit

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

Java Practice Exercise

programmingempire

Princites

Leave a Reply

Your email address will not be published. Required fields are marked *