Science Knowings: JavaScript Course For Social Media

React Component Lifecycle

From Custom Hooks to React Component Lifecycle

Welcome to the session on React Component Lifecycle! In this session, we'll delve into the different phases a React component goes through during its lifetime.

What is the Component Lifecycle?

The component lifecycle refers to the predictable stages that a React component goes through from its creation to its removal from the DOM.

The Four Phases of the Lifecycle

There are four primary phases of the component lifecycle:

  • Mounting
  • Updating
  • Unmounting

1. Mounting: componentWillMount()

The componentWillMount() method is invoked immediately before a component is mounted (inserted into the DOM).

2. Updating: componentWillUpdate()

The componentWillUpdate() method is called just before a component is updated (when its state or props change).

3. Unmounting: componentWillUnmount()

The componentWillUnmount() method is called when a component is about to be removed from the DOM.


This method is invoked when a component receives new props. It's deprecated in favor of getDerivedStateFromProps().


This method is used to determine if a component should update when its state or props change. It returns a true or false value.


This method is used to derive state from props. It returns an object that is merged with the current state.


This method is invoked immediately before a component is updated and can be used to capture a snapshot of the DOM before the update occurs.


This method is used to catch errors in the component tree. It takes an error object and an info object as parameters.

Lifecycle Methods in Class Components

Lifecycle methods are defined as class methods in class components.

class MyComponent extends React.Component { ... }

Lifecycle Methods in Functional Components

Lifecycle methods are defined using hooks in functional components.

const MyComponent = () => { ... }

Custom Lifecycle Methods

You can create your own custom lifecycle methods by defining them as class methods or using hooks.

Best Practices for Using the Lifecycle

Follow these best practices when using the component lifecycle:

  • Use the correct lifecycle method for each task.
  • Avoid side effects in lifecycle methods.
  • Use lifecycle methods for data fetching and cleanup.

Next Up: React Router

In the next session, we'll explore React Router, a powerful library for managing navigation and routing in React applications. Follow us to learn more!