loading...

React fetch data in server before render in 2020

jmhungdev profile image Jimmy ・3 min read

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 is so complex that Facebook even created Relay and Suspence in React 16.6 just to address it. Read the second article to fully understand all data fetching cases and what Facebook is trying to solve.

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>
  }


or you can simplify by using a ternary statement:

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

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

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>
     }
   }

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.

Posted on by:

Discussion

markdown guide