-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLifeCycle.jsx
More file actions
80 lines (65 loc) · 2.03 KB
/
LifeCycle.jsx
File metadata and controls
80 lines (65 loc) · 2.03 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Component } from "react";
class LifeCycle extends Component {
constructor(props){
super(props);
this.state = {
loader: false,
products: []
}
}
fetchProductData = async()=> {
const state = {...this.state}; // state reference now it the value copy
try {
state.loader = true;
console.log('state=>' , state);
this.setState(state)
console.log('state after loader set=>' , this.state);
const res = await fetch("https://dummyjson.com/products");
const result = await res.json();
// console.log(result)
state.products = result?.products;
state.loader = false
this.setState(state) ; // this.setState behave like async
} catch(error) {
state.loader = false;
this.setState(state);
alert("Something went wrong!!! Please try Again!!")
}
}
componentWillMount() {
}
componentDidMount() {
this.fetchProductData();
}
render() {
return (
<div>
{this.state.loader ? (
<div>
<img src="/Loading_icon.gif" />
</div>
) : (
this.state.products.map((pr) => {
// console.log(pr); //
return (
<div>
<h6>{pr.title}</h6>
<div>
<span>Brand::</span> <span>{pr.brand}</span>
</div>
<div>
<span>Price::</span> <span>{pr.price}</span>
</div>
</div>
);
})
)}
</div>
)
}
}
export default LifeCycle;
// Initialiozation => constructor();
//Mounting => componentWillUnmount called jsut before the component mounts on the dom or render function called
// => componenntDidMount => this method called after the component mount on the dom
// api => mounting , intialize , unmonting , update