Skip to content

Commit

Permalink
🌐 HTTP Get Request
Browse files Browse the repository at this point in the history
HTTP Get Request
  • Loading branch information
SaishJ committed Sep 13, 2022
1 parent c925fec commit c33bcb1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ export default UpdatedComponent;

### [Render Peops](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/d60d9bec7747e75247a97d22406520cbac48fa0a)

### [Context]()
### [Context](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/c925fec6db26889ac391d1c8cd072b67e40c2b49)
44 changes: 44 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Expand Down
6 changes: 4 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ import ClickCounterTwo from "./components/ClickCounterTwo";
import HoverCounterTwo from "./components/HoverCounterTwo";
import { UserProvider } from "./components/useContext";
import ComponentA from "./components/ComponentA";
import PostList from "./components/PostList";

function App() {
return (
<div className="App">
<UserProvider value="Saish">
<PostList />
{/* <UserProvider value="Saish">
<ComponentA />
</UserProvider>
</UserProvider> */}
{/* <Increment
render={(count, incrementCount) => (
<ClickCounterTwo count={count} incrementCount={incrementCount} />
Expand Down
42 changes: 42 additions & 0 deletions src/components/PostList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { Component } from "react";
import axios from "axios";

class PostList extends Component {
constructor(props) {
super(props);

this.state = {
posts: [],
errMsg: "",
};
}

componentDidMount() {
axios
.get("https://jsonplaceholder.typicode.com/posts")
.then((response) => {
console.log(response);
this.setState({ posts: response.data });
})
.catch((error) => {
console.log(error);
this.setState({ errMsg: "Error retrieving data..." });
});
}

render() {
const { posts, errMsg } = this.state;
return (
<div>
<h1>Post List</h1>
{posts.length ? (
posts.map((post) => <h3 key={post.id}>{post.title}</h3>)
) : (
<h3>{errMsg}</h3>
)}
</div>
);
}
}

export default PostList;

0 comments on commit c33bcb1

Please sign in to comment.