Component Lifecycle

Component Lifecycle Methods

Mounting

When a class component is being added to the component tree, the following process occurs:

  1. The constructor method is called;
  2. The render method is called;
  3. React updates the DOM; and
  4. The componentDidMount lifecycle method is called.

Updating

A component will update if it receives new props or if the setState method is called.

Unmounting

Just before a class component is removed from the component tree, the componentWillUnmount lifecycle method is called. This is often used for cleaning up any asynchronous functions from our React Component.

Fetching Data from an API

A common use case for the componentDidMount lifecycle method, is to fetch data from an API after a component has been mounted to the DOM. EX:

import React from 'react';

class FetchingData extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      repositories: [],
    };
  }

  async componentDidMount() {
    const url = `https://api.github.com/users/${this.props.gitHubUsername}/repos`;

    const res = await fetch(url);
    const data = await res.json();
    this.setState({ repositories: data });
  }

  render() {
    const { repositories } = this.state;

    if (repositories.length === 0) {
      return (
        <div>Fetching data...</div>
      );
    } else {
     return (
       <div>
         <h2>GitHub Repositories for {this.props.gitHubUsername}</h2>
         <ul>
           {
             repositories.map((repo) => (
               <li key={repo.id}>
                 <a href={repo.html_url}>{repo.name}</a>
               </li>
             ))
           }
         </ul>
       </div>
     );
    }
  }
}

export default FetchingData;

In the above example: