ReactJS — Component Life Cycle

Sona
Webtips
Published in
4 min readOct 20, 2020

--

React Lifecycle

Every component in React goes through a lifecycle of events. You can think is of going through a cycle of birth, growth, and death the same as the picture below.

Human and React Lifecycle comparison
Human and React Lifecycle

The phases are:

  • Initialization — Starting the journey of your component
  • Mounting — Birth of your component
  • Update — Growth of your component
  • Unmount — Death of your component

1. Initialization

This is the phase in which the component is going to start its journey. The developer has to define the props and initial state of the component. This is usually done inside the constructor method (see below to understand the initialization phase better).

class Increment extends React.Component {constructor(props){super(props);// Setting the initial statethis.state = { count : 0};}}

2. Mounting

Mounting is the phase of the component lifecycle when the initialization of the component is completed and the React component mounts on the DOM (i.e., is created and inserted into the DOM) and rendered for the first time on the webpage. It has 2 predefined functions:-

  • componentWillMount(): As the name clearly suggests, This method is invoked just before a component mounts on the DOM, i.e. this function gets invoked once before the render() function is executed for the first time. After this method, the component gets mounted.
  • componentDidMount(): Similarly to the previous one this method is called after the component gets mounted on the DOM and only once in a lifecycle. Before the execution of this method, the render method is called (i.e., we can access the DOM). We can make API calls and update the state with the API response.
class ReactCycle extends React.Component {
componentWillMount() {
console.log('Component will mount!')
}
componentDidMount() {
console.log('Component did mount!')
this.getData();
}
getData=()=>{
/*** method to make api call***
}
render() {
return (
<div>
<h3>Hello mounting methods!</h3>
</div>
);
}
}

3. Updating

Updation is the phase where the states and props of a component are updated followed by some user events such as clicking, pressing a key on the keyboard etc. This is where the component’s state changes and hence, re-rendering takes place.

The methods that are available in this phase are:

  • componentWillRecieveProps() Function: It is invoked as soon as the props are updated before another render is called. Basically, it is invoked before a mounted component receives new props. If you need to update the state in response to prop changes (for example, to reset it), you may compare this.props and nextProps and perform state transitions using this.setState() in this method.
componentWillRecieveProps(newProps){if (this.props !== newProps) {console.log(" New Props have been assigned ");// Use this.setState() to rerender the page.}}
  • shouldComponentUpdate: This will determine if the component will be updated or not. The Function fulfills the requirement by letting React know whether the component’s output will be affected by every state or props update or not. It is invoked before rendering an already mounted component when new props or states are being received. If returned false then the subsequent steps of rendering will not be carried out. This method is not called for the initial render or when forceUpdate() is used. The Function takes the new Props and new State as the arguments and returns whether to re-render or not by returning a true or false value. By default, it is set to true. This method only exists as a performance optimization
  • componentWillUpdate: is called just before rendering i.e. the function gets invoked once before the render() function is executed after the update of State or Props.
  • componentDidUpdate: is called just after rendering. This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).componentDidUpdate() will not be invoked if shouldComponentUpdate() returns false.

4. Unmounting

This is the final phase of the lifecycle of the component that is the phase of unmounting the component from the DOM. The following function is the sole member of this phase.

  • componentWillUnmount(): This function is invoked before the component is finally unmounted and destroyed from the DOM. You should not call setState() in componentWillUnmount() because the component will never be re-rendered. Once a component instance is unmounted, it will never be mounted again. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount()

Thanks for reading. Happy React coding!…

Interested in learning a very important hook. Click the link below.

https://www.knowledgehut.com/blog/web-development/all-about-react-usecallback

--

--

Sona
Webtips

I am Sonia Pahuja, a Meticulous and hard-working FrontEnd Developer with approx. 5 years of work experience majorly working with HTML5, CSS3, React JS, etc.