Rect Lifecycle8
Rect Lifecycle8
Each component in React has a lifecycle which you can monitor and manipulate during
its three main phases.
componentDidMount
The componentDidMount() method is called after the component is rendered.
This is where you run statements that requires that the component is already placed
in the DOM.
shouldComponentUpdate
In the shouldComponentUpdate() method you can return a Boolean value that specifies
whether React should continue with the rendering or not.
componentDidUpdate
The componentDidUpdate method is called after the component is updated in the DOM.
This action triggers the update phase, and since this component has a
componentDidUpdate method, this method is executed and writes a message in the
empty DIV element:
componentWillUnmount
The componentWillUnmount method is called when the component is about to be removed
from the DOM.
constructor(props){
super();
this.state={hello:"React"};
this.changeState=this.changeState.bind(this)
}
render(){
return(
<div>
<h1>React JS Component's Lifecycle </h1>
<h3>Hello{this.state.hello}</h3>
<button onClick={this.changeState}>Click here!</button>
</div>
);
}
componentWillMount(){
console.log('Component will Mount!');
}
componentDidMount(){
console.log('component did Mount');
}
changeState(){
this.setState({hello:"All!! Its great react tutorial."})
}
componentWillReceiveProps(){
console.log('Component will receive props!')
}
shouldComponentUpdate(newProps,newState){
return true;
}
componentWillUpdate(newProps,newState){
console.log('Component will Update!');
}
componentDidUpdate(prevProps,prevState){
console.log('Component did update!');
}
componentWillUnmount(){
console.log('Console will Unmount!!')
}
}
export default Lifecycle;
index.js