Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Using Multiple Fetch Statements with ComponentWillMount In React

The componentWillMount lifecycle method is a relic of the past. Going forward, all HTTP requests should be made from within the componentDidMount lifecycle method.

Nov 9, 2020 • 4 Minute Read

Introduction

When writing code within your React app, you will most likely need to fetch data from an external API in order to render views based on that data. However, introducing asynchronous code into your app always comes with an overhead of complexity. To help combat this complexity, React introduces component lifecycle hooks. Component lifecycle hooks are methods that are triggered at certain phases in a component's "lifecycle." The two most common component lifecycle hooks that have been used in the past when it comes to HTTP requests are componentWillMount and componentDidMount.

In this guide, you will see how componentWillMount and componentDidMount have been used to make HTTP requests in the past. You will also discover how to execute multiple HTTP fetch requests for data within them.

The componentWillMount Lifecycle Hook

componentWillMount used to be an integral part of any component's lifecycle. I say "used to be" because now, componentWillMount is deprecated as of React v. 17! In a lot of apps, it was the lifecycle hook that was used to request data, as the name sort of suggests that this would be the place to do it! However, the problem with this approach was that a component render function was guaranteed to run before the HTTP request(s) was executed within componentWillMount. This often meant that your component had no data to show within its view layer if that data relied upon the HTTP request.

Because of this, you ultimately need to have some sort of default data or loading screen to show until the HTTP request comes back since the render function will end up running before the successful execution of the request. Ultimately, this means that you can drop the use of componentWillMount and simply set up some default data or a loading screen within the constructor of your class component. You can then move your HTTP request over to the componentDidMount lifecycle method. The componentDidMount lifecycle method is guaranteed to run directly after the execution of the first render function. This makes it a reliable choice for fetching data!

Using Multiple Fetch Statements Within componentDidMount

In this section, you will learn how to make HTTP requests using the Fetch API from two different locations and merge the results. Let's jump into the code!

      import React from 'react';

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

        this.state = {
            mergedData: this.props.defaultData
        }
    }

    componentDidMount() {
        Promise.all([
            fetch(this.props.urlOne).then(res => res.json()),
            fetch(this.props.urlTwo).then(res => res.json())
        ]).then(([urlOneData, urlTwoData]) => {
            this.setState({
                mergedData: urlOneData.concat(urlTwoData)
            });
        })
    }

    render() {
        return (
            <div>
                <h1>Check out this Data!</h1>
                <DataView data={this.state.mergedData}/>
            </div>
        )
    }
}
    

In the trivial example above, you can see the MyComponent component. This component is getting some default data from props via a parent component. This is the data that will be shown initially.

Then in the componentDidMount lifecycle method, multiple fetch statements are being executed against two different APIs. The Promise.all method is used to combine the results of these calls to fetch into a single array. Using Promise.all allows the component to execute these requests in parallel. Once both responses complete, you will have an array containing two sub-arrays. The setState method is then called in order to set the mergedData state based on the concatenated result of the two sub-arrays.

And there you have it! You just learned how to use the Fetch API to execute multiple HTTP requests from within the componentDidMount lifecycle method.

Conclusion

The componentWillMount lifecycle method is a relic of the past. Please use it with caution! Going forward, all HTTP requests should be made from within the componentDidMount lifecycle method.

There is also another way to fetch data if you are using a functional component instead of a class component. That way is to use the useEffect hook. For more information regarding useEffect, please check out the React documentation.

Zachary Bennett

Zachary B.

Zach is currently a Lead Software Developer at OpalSoft where he uses tools such as Scala, TypeScript, Python, Docker, Node, and Angular. Zach has a passion for GIS programming along with open-source software. You can view some of his work on GitHub (https://github.com/zbennett10) and Stack Overflow (https://stackoverflow.com/users/6879849/zachary-bennett).

More about this author