Open In App

What are the different phases of ReactJS component lifecycle ?

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.



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:

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:

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. 

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 Structure

The 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.




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:


Article Tags :