A guide to React component lifecycle methods.

A guide to React component lifecycle methods.

·

3 min read

Understanding the lifecycle of components is an integral part in mastering React. For a very long time, React development has relied heavily on React component lifecycle methods.

At various points in the component's life cycle, they aid developers in managing the behaviour and state of the component.

Although the management of components has altered with the introduction of React Hooks, the useEffect hook, in particular, the lifecycle methods continue to be extremely important.

In this article, we will cover the different lifecycle methods available in React and how they can be implemented using React Hooks.

  1. componentDidMount(): This method is called after the component is rendered on the DOM. It is used to fetch data from APIs or perform any setup that requires the component to be fully rendered. It is used to carry out any setup that necessitates a fully displayed component.

    Here's how you can implement componentDidMount using useEffect hook:

     import React, { useEffect } from 'react';
    
     const MyComponent = () => {
       useEffect(() => {
         console.log('Component did mount');
         // Fetch data here
       }, []);
    
       return (
         <div>
           {/* component code here */}
         </div>
       );
     };
    

    It is characterized by injecting an empty dependency array into our hook.

  2. componentDidUpdate(): When the component is updated, this method is invoked.

    It is used to either execute any cleanup or to change the component's state based on the updated props.

    Here's an implementation.

     import React, { useEffect } from 'react';
    
     const MyComponent = (props: { id: number }) => {
       useEffect(() => {
         console.log('Component did update');
         // Perform cleanup here
       }, [props.id]);
    
       return (
         <div>
           {/* component code here */}
         </div>
       );
     };
    

    As the above snippet shows, we pass in a prop and use the prop as a "checker" to monitor when the useEffect triggers.

  3. componentWillUnmount(): This method is called before the component is unmounted from the DOM. It is usually used to perform any cleanup before the component is removed. Examples of cleanups are clearing setIntervals, setTimeOuts, unsubscribing from events, cancelling API calls, etc

Below is an implementation:

import React, { useEffect } from 'react';

const MyComponent = () => {
  useEffect(() => {
    return () => {
      console.log('Component will unmount');
      // Perform cleanup here
    };
  }, []);

  return (
    <div>
      {/* component code here */}
    </div>
  );
};

As seen in the code snippet, a return function is called in our useEffect , which performs a "clean-up" on our DOM , thus unrendering our component.

In conclusion, lifecycle methods are still relevant, even with the introduction of React Hooks. They provide a way for developers to manage the behavior and state of components at different stages of the component's lifecycle. By using them efficiently, developers can create more maintainable and scalable applications.