- ReactJS component has 7 life cycle methods and has the callback for each phase of React component starting from loading to mounting to un-mounting.
- In this demo, “we will learn about shouldComponentUpdate lifecycle methods”.
- The following code chows the use of all 7 lifecycle methods of React component.
import React from 'react'; class WeatherComponent extends React.Component { constructor(props) { super(props); this.state = { weather: "Sunny", }; } //LifeCycle method 1 componentWillMount(){ console.log("inside componentWillMount method"); } //LifeCycle method 2 componentDidMount(){ console.log("inside componentDidMount method"); } //LifeCycle method 3 componentWillReceiveProps(){ console.log("inside componentWillReceiveProps method"); } //LifeCycle method 4 shouldComponentUpdate(){ console.log("inside shouldComponentUpdate method"); return true; } //LifeCycle method 5 componentWillUpdate(){ console.log("inside componentWillUpdate method"); } //LifeCycle method 6 componentDidUpdate(){ console.log("inside componentDidUpdate method"); } //LifeCycle method 7 componentWillUnmount(){ console.log("inside componentWillUnmount method"); } changeName() { this.setState({weather:"Rainy"}) } render() { console.log("inside render method"); return ( <div> <h1>Today Weather is : {this.state.weather}</h1> <button onClick={this.changeName.bind(this)}> Change weather to Rainy </button> </div> ); } } export default WeatherComponent;
- shouldComponenUpdate() method returns a Boolean value. If it returns false, then the states of the component can not be modified.If it returns true, then the states of the component can be modified.
- In this example we have returned true value to make state weather modifiable.The following screenshot shows the output of initial load.
- when user clicks on the button the state of the weather will change to Rainy. the following screenshot shows the modified state.