Master raj
3 min readMay 5, 2023

Fetch API : Again are you using that right🫸

The Fetch API is a powerful tool that allows you to send HTTP requests to servers and receive responses in JavaScript. It is often used in React to fetch data from APIs and render it in the user interface.

Now we will See The Graphical Explanation of Fetch API.

Than @at last I have given one example of my own with detailed explanation make sure to checkout that.

1
2
3
4
5
6
7

So above I have tried to explain in a way so that you guys won’t get bored 🥱 and stay tuned with learning.

Jara diary — YouTube

Errormania — YouTube

Connect with me on telegram:-

We are dedicated to MERN stack projects. We share tutorials, tips, and project showcases. Join us today to stay Updated
https://t.me/nodereact

Here's an example of How to use the Fetch API in React to fetch data from an external API:

import React, { useState, useEffect } from "react";

function App() {
const [data, setData] = useState([]);

useEffect(() => {
const fetchData = async () => {
const response = await fetch("https://jsonplaceholder.typicode.com/posts");
const json = await response.json();
setData(json);
};

fetchData();
}, []);

return (
<div>
<h1>Posts</h1>
<ul>
{data.map((post) => (
<li key={post.id}>
<h2>{post.title}</h2>
<p>{post.body}</p>
</li>
))}
</ul>
</div>
);
}

export default App;

Explanation:

In this example, we use the useState hook to create a state variable called data that will hold the data fetched from the API. We also use the useEffect hook to fetch the data when the component mounts.

The fetchData function is an asynchronous function that uses the Fetch API to send a GET request to the external API and retrieves the response in JSON format. The setData function is then used to update the state with the retrieved data.

Finally, we use the map function to render the data in the user interface by mapping over the data array and rendering each post as an li element with its title and body.

This is just a basic example of how to use the Fetch API in React. There are many different use cases for the Fetch API, such as handling errors, sending data with different HTTP methods, and using query parameters. Depending on your specific use case, you may need to modify the code to suit your needs

Thank You so Much Happy Learning 😊

Master raj

Certified MERN Stack Developer from Infosys, now sharing expertise on #InCloudByRK. Proficient in MERN Stack, AWS, GCP, Azure DevOps. Let's level up!