-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifecycle1.js
53 lines (47 loc) · 1.5 KB
/
lifecycle1.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
50
51
52
53
import React from "react";
class Lifecycle extends React.Component
{
constructor(props)
{console.log("in Constructor",props.id)
super(props)
this.state={counter:0}
}
componentDidMount()
{
console.log("in ComponentDidMount")
}
handleClick=(val)=>{
console.log(val)
if(val==="+") this.setState((prevState)=>{return{counter:prevState.counter+1}})
else if(val==="-") this.setState((prevState)=>{return{counter:prevState.counter-1}})
else this.setState({counter:0})
}
shouldComponentUpdate()
{ console.log("in shouldComponentUpdate")
return true
}
componentDidUpdate(){
console.log("in ComponentDidUpdate")
}
componentWillUnmount()
{
console.log("in componentWillUnmount")
}
render()
{
console.log("in render",this.props.id)
return(
<>
<h1>Child component</h1>
<h2>Counter :{this.state.counter}</h2>
{/* passing arguments via arrow function */}
<button onClick={()=>this.handleClick("+")}>Increment</button>
{/* passing arguments via bind() */}
<button onClick={this.handleClick.bind(this,"-")}>Decrement</button>
<button onClick={()=>this.handleClick("")}>Reset</button>
<button onClick={()=>this.forceUpdate()}>forceUpdate()</button>
</>
)
}
}
export default Lifecycle