Created
July 13, 2022 21:25
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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