Skip to content

Instantly share code, notes, and snippets.

@olawanlejoel
Created July 13, 2022 21:25
Show Gist options
  • Select an option

  • Save olawanlejoel/fbc13f71d8ec4d1a9ddc5a65cf2caaee to your computer and use it in GitHub Desktop.

Select an option

Save olawanlejoel/fbc13f71d8ec4d1a9ddc5a65cf2caaee to your computer and use it in GitHub Desktop.
How To Perform GET HTTP Request in React’s Functional Component With Fetch API
import { useState, useEffect } from "react";
const App = () => {
const [posts, setPosts] = useState([]);
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/posts")
.then((res) => res.json())
.then((data) => {
console.log(data);
setPosts(data);
})
.catch((err) => {
console.log(err.message);
});
}, []);
return (
<div className="posts-container">
{posts.map((post) => {
return (
<div className="post-card" key={post.id}>
<h2 className="post-title">{post.title}</h2>
<p className="post-body">{post.body}</p>
<div className="button">
<div className="delete-btn">Delete</div>
</div>
</div>
);
})}
</div>
);
};
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment