DEV Community

loading...

React fetching data before rendering in 2020

jmhungdev profile image Jimmy Updated on ・3 min read

TLDR:
Do not fetch data in ComponentWillMount, do it in ComponentDidMount

There are few use cases in React project where you think you need to fetch data before rendering.

When you quickly google 'fetching data before first rendering in React', this is the first answer that popped up from StackOverlflow.

The most voted answer which suggests to do in componentWillMount(), a method fired off before render() in React lifecycle will be completely deprecated in React v17 (as of current writing on 5/10/2020 React is at version 16.13.1).

The mechanism of fetching data in React application complex enough that Facebook created Relay and Suspence in React 16.6 to address it. Read the second article to fully understand all use cases.

Here I present some issues that one think they need to fetch data before rendering, but it's not always the case.

It is best to keep your data fetching in componentDidMount()
(it's okay to have first renders with no data)

Issues:

  1. The render gets compile error when data is not found
  2. A child component render relies on data response from parent component
  3. A child component that has heavy synchronous code relies on data response from parent component

Cases such as 1 and 2. You can implement an if statement to conditionally render based on if your data returned or not.


  if( dataIsReturned === true) {
    <RenderYourData/> 
  } else {
   <h1> Loading </h1>
  }


Enter fullscreen mode Exit fullscreen mode

or you can simplify by using a ternary statement:

 {
  dataIsReturned ? <RenderYourData/> : <h1> Loading </h1>
 }

Enter fullscreen mode Exit fullscreen mode

In case #3 usually comes from a design fault to start with. But because of it is legacy code, it would take too much resource to refactor. Usually the problem is presented this way.


   //start of legacyChild.js

   let initiator = init(data);

   // 5000 lines of code

   function LegacyChild () = {
     return initiator;
   }

   export LegacyChild;
   //end of legacyChild.js




   //start of parent.js

   import <LegacyChild/> from './legacyChild';

   class Parent extends React.Component {

     componentDidMount(){
      fetch()
       .then(this.setState(data));
     }

     render() {
        <LagacyChild/>
     }
   }

   React.render(<Parent/>, document.getElementById('root');
   //end of parent.js
Enter fullscreen mode Exit fullscreen mode

Notice two things:

  1. legacyChild.js contains code outside of exported function, in this case there are 5000 lines of synchronous javascript code that is impossible to refactor and they depend on an external data source outside of legacyChild.js. The function LegacyChild's definition depends upon those 5000 lines of code, by leveraging the power of closure.

  2. In parent.js, legacyChild.js is imported at the top of parent.js, it will result in a compiled error because it reads legacyChild.js which depends on the data response in Parent component.

In this case you can delay the import of legacyChild.js by using dynamic import.

 class Parent extends React.Component {

     constructor() {

      this.state = {
        dataIsReturned : false ;
      }
      this.LegacyChild = null;
     } 

     componentDidMount(){
      fetch()
       .then(async (data) => {
          let obj = await import('./legacyChild');
          this.LegacyChild = obj.default;
          this.setState({dataIsReturn : true});
       ).catch( err => console.error(err);)
     }

     render() {
        if dataIsReturned ? <this.LagacyChild/> : <h1> Loading </h1>
     }
   }
Enter fullscreen mode Exit fullscreen mode

Here we only changed the order of importing legacyChild.js, we still got to fetch the data based on official recommendation, inside componentDidMount().

Let me know other use case in the comment that I didn't mentioned where it is forced to fetch data before rendering.

Discussion (1)

pic
Editor guide
Collapse
greenon profile image
Pramod Vadrevu

Hi, Thanks for this article. I think there is a typo in the above code. It should be dataIsReturned and not dataIsReturn in the componentDidMount() block.