-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Component Lifecycle Methods
- Loading branch information
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { Component } from "react"; | ||
import LifecycleB from "./LifecycleB"; | ||
|
||
class LifecycleA extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
name: "Saish", | ||
}; | ||
console.log("LifecycleA constructor"); | ||
} | ||
|
||
static getDerivedStateFromProps(props, state) { | ||
console.log("LifecycleA getDerivedStateFromProps"); | ||
return null; | ||
} | ||
|
||
componentDidMount() { | ||
console.log("LifecycleA componentDidMount"); | ||
} | ||
|
||
shouldComponentUpdate() { | ||
console.log("LifecycleA shouldComponentUpdate"); | ||
return true; | ||
} | ||
|
||
getSnapshotBeforeUpdate(prevProps, prevState) { | ||
console.log("LifecycleA getSnapshotBeforeUpdate"); | ||
return null; | ||
} | ||
|
||
componentDidUpdate() { | ||
console.log("LifecycleA componentDidUpdate"); | ||
} | ||
|
||
changeState = () => { | ||
this.setState({ | ||
name: "SJ", | ||
}); | ||
}; | ||
|
||
render() { | ||
console.log("LifecucleA render"); | ||
return ( | ||
<div> | ||
<h1>LifecycleA</h1> | ||
<button onClick={this.changeState}>Change State</button> | ||
<LifecycleB /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default LifecycleA; | ||
|
||
// Component Lifecycle Methods: | ||
/* | ||
1) Mounting:- When an instance of a component is being created and inserted into the DOM. | ||
Methods:- constructor, static getDerivedStateFromProps, render, componentDidMount | ||
2) Updating:- When a component is being re-renedred as a result of changes to either its props or state. | ||
Methods:- static getDerivedStateFromProps, shouldComponentUpdate, render, | ||
getSnapshotBeforeUpdate, componentDidUpdate | ||
3) Unmounting:- When a component is being removed from DOM. | ||
Methods:- componentWillUnmount | ||
4) Error Handling:- When there is an error during rendering in a lifecycle method or in the | ||
constructor of any child component. | ||
Methods:- static getDerivedStateFromError, componentDidCatch | ||
*/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { Component } from "react"; | ||
|
||
class LifecycleB extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
name: "Saish", | ||
}; | ||
console.log("LifecycleB constructor"); | ||
} | ||
|
||
static getDerivedStateFromProps(props, state) { | ||
console.log("LifecycleB getDerivedStateFromProps"); | ||
return null; | ||
} | ||
|
||
componentDidMount() { | ||
console.log("LifecycleB componentDidMount"); | ||
} | ||
|
||
shouldComponentUpdate() { | ||
console.log("LifecycleB shouldComponentUpdate"); | ||
return true; | ||
} | ||
|
||
getSnapshotBeforeUpdate() { | ||
console.log("LifecycleB getSnapshotBeforeUpdate"); | ||
return null; | ||
} | ||
|
||
componentDidUpdate() { | ||
console.log("LifecycleB componentDidUpdate"); | ||
} | ||
|
||
render() { | ||
console.log("LifecucleB render"); | ||
return ( | ||
<div> | ||
<h1>LifecycleB</h1> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default LifecycleB; |