• Articles
  • Tutorials
  • Interview Questions

React Component Lifecycle

React Component Lifecycle

In this blog, we will explore the React component lifecycle in detail. We will delve into each phase, discuss the purpose and usage of individual lifecycle methods, and provide practical examples to illustrate their significance.

Following are the topics we are going to cover

Watch this video on ReactJS Full Course:

Video Thumbnail

What is the React Component Lifecycle?

The React component lifecycle refers to the series of events and methods that occur during the lifespan of a React component. It outlines the various stages a component goes through, from its creation and insertion into the DOM to its removal from the DOM. The react lifecycle can be divided into three main phases: mounting, updating, and unmounting.

Understanding the React component lifecycle is crucial for developers as it provides a way to manage and control the behavior of components at each stage of their existence. By utilizing the lifecycle methods effectively, developers can perform necessary setup and cleanup tasks, optimize rendering, and ensure the proper handling of component updates and unmounting.

Here’s an example that demonstrates the React component lifecycle in action:

import React, { Component } from 'react';
class ExampleComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
     count: 0,
    };
  }
  componentDidMount() {
    console.log('Component is mounted');
  }
  componentDidUpdate(prevProps, prevState) {
    if (prevState.count !== this.state.count) {
      console.log('Count state has been updated');
    }
  }
  componentWillUnmount() {
    console.log('Component is about to be unmounted');
  }
  increaseCount() {
    this.setState((prevState) => ({
      count: prevState.count + 1,
    }));
  }
  render() {
    return (
      <div>
        <h1>Component Lifecycle Example</h1>
        <p>Count: {this.state.count}</p>
        <button onClick={() => this.increaseCount()}>Increase Count</button>
      </div>
    );
  }
}
export default ExampleComponent;

In the above mentioned example, we can observe a straightforward class-based component called ExampleComponent. It starts by initializing the component state with a count value set to 0.

During the mounting phase, the componentDidMount() method is invoked, triggering the execution of a console.log statement that confirms the component has been successfully mounted.

Upon clicking the button and triggering the increaseCount() method, the component’s state is updated, thus initiating the updating phase. In the componentDidUpdate() method, a comparison is made between the previous count value and the current count value. If they differ, a message is logged to the console to indicate that the count state has been updated.

In the event that the component is unmounted, such as when navigating away from the page, the componentWillUnmount() method is called, and a message is logged to the console, signifying that the component is on the verge of being unmounted.

The provided example serves as a practical demonstration of how the sequence of react lifecycle methods unfolds as the component is mounted, updated, and potentially unmounted. Through this, a better understanding of the React component lifecycle and its functioning in real-world scenarios can be gained.

Get 100% Hike!

Master Most in Demand Skills Now!

Why Do We Need React Component Lifecycle?

Here are some compelling reasons highlighting the importance of the React component lifecycle:

  • Initialization and Setup: During the mounting phase, the component lifecycle allows developers to perform initialization tasks, such as setting the initial state, binding event handlers, or fetching initial data. These tasks ensure that the component is properly set up before it is rendered.
  • Data Fetching and Side Effects: The utilization of component lifecycle methods, including componentDidMount and componentDidUpdate, presents developers with valuable opportunities to handle asynchronous operations, interact with external libraries, and effectively manage side effects. 

These methods are instrumental in ensuring that asynchronous tasks are executed in a controlled and predictable manner. They enable seamless integration with external resources, and facilitate the management of complex side effects within components.

  • Rendering Optimization: The updating phase of the component lifecycle enables developers to optimize rendering by implementing the shouldComponentUpdate method. By comparing the current props and state with the next props and state, developers can determine whether a component needs to be re-render. This optimization can significantly improve the performance of React applications by reducing unnecessary rendering.
  • Cleanup and Resource Management: The component lifecycle includes the componentWillUnmount method, which allows developers to perform cleanup tasks before a component is unmounted. This can involve canceling timers, removing event listeners, or releasing any resources held by the component. Proper cleanup ensures that the application remains efficient and avoids memory leaks or unexpected behavior.

Phases of React Component Lifecycle

The React component lifecycle encompasses three primary phases: mounting, updating, and unmounting, each representing a distinct stage in the life cycle of a component.

Mounting Phase

When a component is generated and added to the DOM, this stage takes place. It uses the following techniques:

  • constructor(): The initial state of the component can be defined and event handlers can be bound using this method, which is invoked when the component is initialized.
  • static getDerivedStateFromProps(props, state): Before rendering, this method is called to empower the component to modify its state according to any alterations in the props.
  • render(): This method returns the JSX markup to be rendered on the screen.
  • componentDidMount(): Right after the component is successfully mounted in the DOM, this method is invoked and commonly utilized for essential initialization tasks like fetching data or establishing subscriptions.

Here’s an example of a React component with the Mounting phase lifecycle methods:

import React, { Component } from 'react';
class MyComponent extends Component {
  constructor(props)
    super(props);
    console.log('Constructor');
  }
  componentDidMount() {
    console.log('Component Did Mount');
  }
  render() {
    console.log('Render');
    return (
      <div>
        {/* JSX content */}
      </div>
    );
  }
}
export default MyComponent;

During the Mounting phase, the following methods are involved:

  • Constructor: The initialization of the component includes tasks such as setting the initial state, binding event handlers, and performing other necessary setup operations. It is considered good practice to invoke super(props) at the beginning of the constructor to ensure proper inheritance of props.
  • Render: Generates JSX and renders it to the DOM. It should be a pure function without side effects.
  • Component Did Mount: Executes immediately after the component is mounted and rendered to the DOM. Commonly used for tasks like API requests, setting up event listeners, or other setup operations requiring DOM access.

The order of execution during the Mounting phase is as follows:

  • Constructor
  • Render
  • Component Did Mount

Note that React components may proceed to subsequent lifecycle phases, such as Updating and Unmounting, depending on prop or state changes or when the component is unmounted from the DOM.

Updating Phase

When a component’s state or props change, a new render is initiated during this phase. The techniques used are as follows:

  • static getDerivedStateFromProps(props, state): This function is used to enable the component to update its state in response to changes in props before re-rendering.
  • shouldComponentUpdate(nextProps, nextState): By comparing the previous props and state with the current props and state, this method determines whether the component should undergo re-rendering. It serves as a means to enhance productivity by optimizing the rendering process.
  • render(): This method re-renders the component’s JSX markup.
  • getSnapshotBeforeUpdate(prevProps, prevState): Just before the changes resulting from the component update are applied to the DOM, this method is called and offers the opportunity to capture information from the DOM before it undergoes modifications.
  • componentDidUpdate(prevProps, prevState, snapshot): Once the component has been successfully updated in the DOM, this method is called and commonly employed to execute side effects or update the UI in response to changes in the state or props of the component.

Here’s an example of a React component with the Updating phase lifecycle methods:

import React, { Component } from 'react';
class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }
  componentDidMount() {
    console.log('Component Did Mount');
  }
  componentDidUpdate(prevProps, prevState) {
    console.log('Component Did Update');
    console.log('Previous Props:', prevProps);
    console.log('Previous State:', prevState);
  }
  incrementCount() {
    this.setState({ count: this.state.count + 1 });
  }
  render() {
    console.log('Render');
    return (
      <div>
        <h1>Count: {this.state.count}</h1>
        <button onClick={() => this.incrementCount()}>Increment</button>
      </div>
    );
  }
}
export default MyComponent;

In this example, the Updating phase includes the following method:

Component Did Update: This method is called after the component has been updated and re-rendered due to changes in props or state. It receives the previous props and previous state as parameters, allowing you to compare them with the current props and state and perform any necessary side effects.

During the Updating phase, the order of execution is as follows:

  • Render
  • Component Did Update

The console.log statements in the example demonstrate the order of execution during the Updating phase. Note that the Component Did Mount method is not called during the Updating phase as it is specific to the Mounting phase. Keep in mind that React components may go through other lifecycle phases, such as the Mounting and Unmounting phases, depending on their lifecycle events.

Unmounting Phase

The unmounting phase is triggered when a component is on the verge of being removed from the DOM. This phase includes the componentWillUnmount() method, which is executed immediately prior to the component’s unmounting. 

It provides developers with the ability to perform important cleanup tasks, such as canceling timers, detaching event listeners, or releasing any resources tied to the component. These cleanup tasks ensure the component’s graceful removal from the application and prevent any potential memory leaks or unwanted side effects. Here’s an example of a React component with the Unmounting phase lifecycle method: 

import React, { Component } from 'react';
class MyComponent extends Component {
  componentDidMount() {
    console.log('Component Did Mount');
  }
  componentWillUnmount() {
    console.log('Component Will Unmount');
  }
  render() {
    return (
      <div>
        {/* JSX content */}
      </div>
    );
  }
}
export default MyComponent;

In this example, the Unmounting phase includes the following method:

Component Will Unmount: The component is unmounted and deleted from the DOM just before this method is called. It can be used to carry out any required cleanup actions, such as stopping timers, getting rid of event listeners, or canceling subscriptions.

The console.log statement in the example demonstrates the execution of the Component Will Unmount method during the Unmounting phase. Please note that the Unmounting phase occurs when the component is no longer needed or when the parent component is being re-rendered without the child component.

Benefits of React Component Lifecycle

  • Efficient Handling of Asynchronous Operations: The component lifecycle methods, such as componentDidMount and componentDidUpdate, empower developers to effectively manage asynchronous operations like data fetching from APIs or making AJAX requests. This ensures that data is fetched and updated in a controlled manner, mitigating issues like race conditions and maintaining consistent UI states.
  • Effective Error Handling: The React component lifecycle offers methods like componentDidCatch, which enable developers to handle and manage errors within components. This proactive approach helps prevent application crashes and provides the opportunity to display a fallback UI or log error information for effective debugging.
  • Seamless Integration with External Libraries: Leveraging the component lifecycle methods allows for smooth integration of React components with third-party libraries or frameworks. Developers can initialize, update, and clean up external dependencies in sync with the lifecycle of React components, enhancing flexibility and extensibility in building React applications.
  • Promotes Code Reusability: The react lifecycle methods facilitate the encapsulation of specific behaviors and logic within components, promoting code reusability. This allows developers to easily reuse components across different parts of an application, ensuring consistent behavior and reducing code duplication.

Conclusion

As React continues to evolve, there may be advancements and additions to the component lifecycle, offering even more flexibility and control to developers. By leveraging the component lifecycle, developers can efficiently handle asynchronous operations, such as data fetching, and manage side effects in a controlled and predictable manner. The lifecycle methods also enable effective error handling, allowing for graceful recovery and the display of fallback UIs in case of errors.

Course Schedule

Name Date Details
Web Development Courses 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.