Last active
July 13, 2022 21:18
-
-
Save olawanlejoel/ad519ee32bb8bb76b867e94566e975bd to your computer and use it in GitHub Desktop.
How To Perform GET HTTP Request in React’s Class 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 React, { Component } from "react"; | |
| class App extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| posts: [] | |
| }; | |
| } | |
| componentDidMount() { | |
| fetch("https://jsonplaceholder.typicode.com/posts") | |
| .then((response) => response.json()) | |
| .then((data) => this.setState({ posts: data })) | |
| .catch((error) => console.log(error)); | |
| } | |
| render() { | |
| const { posts } = this.state; | |
| 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