-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartB.js
49 lines (43 loc) · 1014 Bytes
/
PartB.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React, { Component } from "react";
import axios from "axios";
class PartB extends Component {
constructor(props) {
super(props);
this.state = {
posts: [],
errorMsg: ""
};
}
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({ errorMsg: "Wrong API call !!!" });
});
}
render() {
const { posts, errorMsg } = this.state;
return (
<div>
<ul>
<div>
{posts.length
? posts.map(post => (
<li>
<div key={post.id}>{post.title}</div>
</li>
))
: null}
{errorMsg ? <div className="glow">{errorMsg}</div> : null}
</div>
</ul>
</div>
);
}
}
export default PartB;