All files / src/app App.jsx

100% Statements 11/11
100% Branches 4/4
100% Functions 3/3
100% Lines 11/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                          1x   1x                 9x 2x         2x               9x 4x 4x 2x             2x                     10x                  
/* eslint-disable */
/*
 * © Jordan Tart https://github.com/jtart
 * https://github.com/jtart/react-universal-app
 */
import { Component } from 'react';
import { renderRoutes } from 'react-router-config';
import { withRouter } from 'react-router-dom';
 
import loadInitialData from '../utilities/loadInitialData';
 
export class App extends Component {
  constructor(props) {
    super(props);
 
    this.state = {
      data: this.props.initialData,
      loading: false,
      error: null,
      loadInitialDataPromise: null,
    };
  }
 
  async componentDidUpdate({ location: prevLocation }) {
    if (this.props.location.pathname !== prevLocation.pathname) {
      const initialData = loadInitialData(
        this.props.location.pathname,
        this.props.routes,
      );
 
      this.setState({
        data: null,
        loading: true,
        error: null,
        loadInitialDataPromise: initialData,
      });
    }
 
    if (this.state.loading) {
      try {
        const data = await this.state.loadInitialDataPromise;
        this.setState({
          data,
          loading: false,
          error: null,
          loadInitialDataPromise: null,
        });
      } catch (error) {
        this.setState({
          data: null,
          loading: false,
          error,
          loadInitialDataPromise: null,
        });
      }
    }
  }
 
  render() {
    return renderRoutes(this.props.routes, {
      data: this.state.data,
      loading: this.state.loading,
      error: this.state.error,
    });
  }
}
 
export default withRouter(App);