What are the different phases of ReactJS component lifecycle ?
Last Updated :
04 Dec, 2023
In React JS, the development of each component involves the use of different lifecycle methods. All the lifecycle methods used to create such components, together constitute the component's lifecycle. They are defined as a series of functions invoked in various stages of a component.
There are primarily 4 phases of a lifecycle as follows.
Phase 1: Initializing
This is the initial phase of the React component lifecycle. As the name suggests, this phase involves all the declarations, definitions, and initialization of properties, default props as well as the initial state of the component required by the developer.
- getDefaultProps(): The method invoked immediately before the component is created or any props from the parent component are passed into the said (child) component.
- getInitialState(): The method invoked immediately before the component is created and used to specify the default value of the state.
Phase 2: Mounting
The second phase of the React component lifecycle, followed by the initialization phase, is the mounting phase. It commences when the component is positioned over the DOM container and rendered on a webpage. It consists of 2 methods, namely:
- componentWillMount(): The method invoked immediately before the component is positioned on the DOM, i.e. right before the component is rendered on the screen for the very first time.
- componentDidMount(): The method invoked immediately after the component is positioned on the DOM, i.e. right after the component is rendered on the screen for the very first time.
Phase 3: Updating
The third phase of the ReactJS Component Lifecycle is the Updation phase. It is also responsible for handling user interaction and passing data within the component hierarchy. Some of the lifecycle methods falling into this category are as follows:
- componentWillReceiveProps(): The method invoked immediately before the props of a mounted component get reassigned.
- shouldComponentUpdate(): The method invoked before deciding whether the newly rendered props are required to be displayed on the webpage or not.
- componentWillUpdate(): The method invoked immediately before the component is re-rendered after updating the states and/or properties.
- componentDidUpdate(): The method invoked immediately after the component is re-rendered after updating the states and/or properties.
Phase 4: Unmounting
Unmounting is the last phase of the ReactJS component lifecycle. This phase includes those lifecycle methods which are used when a component is getting detached from the DOM container. It is also responsible for performing the required cleanup tasks. Once unmounted, a component can not be re-mounted again.
- componentWillUnmount(): The method invoked immediately before the component is removed from the DOM at last, i.e. right when the component is completely removed from the page and this shows the end of its lifecycle.
Steps to create React Application And Installing Module:
Step 1: Create a new react application using the following command:
npx create-react-app demo-app
Step 2: Move into your project directory using the following command:
cd demo-app
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example: Now write down the following code in the App.js file.
JavaScript
import React, { Component } from "react";
class App extends Component {
constructor(props) {
super(props);
this.state = { myState: "GeeksforGeeks" };
this.changeMyState =
this.changeMyState.bind(this);
}
render() {
return (
<div style=
{
{
textAlign: "center",
marginTop: "5%", color: "#006600"
}
}>
<h1>Phases of ReactJS Component Lifecycle</h1>
<h3> {this.state.myState}</h3>
<button
onClick={this.changeMyState}>
Click Me!
</button>
</div>
);
}
componentWillMount() {
console.log("Phase 2: MOUNTING -> Component Will Mount!");
}
componentDidMount() {
console.log("Phase 2: MOUNTING -> Component Did Mount!");
}
// Changing in state
changeMyState() {
this.setState({
myState:
"GeeksforGeeks Tutorial on Phases of ReactJS Lifecycle Methods!",
});
}
// Props receiver function
componentWillReceiveProps(newProps) {
console.log("Phase 3: UPDATING -> Component Will Receive Props!");
}
shouldComponentUpdate(newProps, newState) {
// Phase 3: UPDATING
return true;
}
// Updation of component
componentWillUpdate(nextProps, nextState) {
console.log("Phase 3: UPDATING -> Component Will update!");
}
componentDidUpdate(prevProps, prevState) {
console.log("Phase 3: UPDATING -> Component Did update!");
}
// Unmount of component
componentWillUnmount() {
console.log("Phase 3: UNMOUNTING -> Component Will unmount!");
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output:

Similar Reads
What is the difference between React Components in ES5 and ES6 ?
In this article, we will learn about difference between ES5 and ES6 React Components by comparing their individual components and features Table of Content ES5 ComponentsES5 FeaturesES6 ComponentsES6 FeaturesDifference between React Components in ES5 and ES6 ES5 ComponentsES5 (ECMAScript 5) is a sta
5 min read
Different ways to access props inside a Component in React
The props keyword is the shorthand for properties. It is one of the most important features of React which helps in effective communication between various React components. It is a read-only attribute for passing data from the parent component to the child component. There are various ways to acces
4 min read
What is the purpose of the componentDidUpdate lifecycle method ?
React web apps are actually a collection of independent components that run according to the interactions made with them. Every React Component has a lifecycle of its own, the lifecycle of a component can be defined as the series of methods that are invoked in different stages of the componentâs exi
3 min read
What is the Difference between Element and Component ?
An Element is an object that represents a DOM node it is a part of DOM structure, while a component is a reusable block of code that contains logic, states, and also returns the Element. The element contains the information to be rendered on the UI and the Components are composed of the elements. Ta
4 min read
What does it mean for a component to be mounted in ReactJS ?
For a component to be mounted in ReactJS it means to be initialized and inserted in the DOM. Mounting is the initial phase in which the instance of the component is created and inserted into the DOM. When the component gets successfully inserted into the DOM, the component is said to be mounted. In
2 min read
How are React Hooks different from class components in ReactJS?
React Hooks are helpful tools that make building websites and apps with React easier. They simplify how users handle things like data and app behavior, making the code cleaner and easier to understand. Class components in React are like the old-school way of building parts of your website or app. Th
2 min read
What is a pure functional component in ReactJS ?
What is a JavaScript function? A JavaScript function is a block of code designed to perform a particular task which is executed when it is called. How React functional components are similar to JavaScript functions? Conceptually, components are like JavaScript functions. Â A functional component is a
3 min read
What is the significance of the componentDidMount lifecycle method?
Every React Component has a lifecycle of its own, the lifecycle of a component can be defined as the series of methods that are invoked in different stages of the componentâs existence. In this article, we will dive into the componentDidMount and see the significance of it. Prerequisites:NPM & N
3 min read
Difference between React.Component and React.PureComponent?
A Component is one of the core building blocks of React. In other words, we can say that every application you will develop in React will be made up of pieces called components. But React has two types of Components: React.PureComponent: It is one of the most significant ways to optimize React appli
3 min read
What are the differences between Redux and Flux in ReactJS ?
During the phase of applications or software development, we gather the requirements of customers to create a solution to solve the problem of customers or businesses. To solve problems we rely on different technologies and architecture patterns. for a long time, developers were using MVC (Model-Vie
10 min read