import * as React from 'react' ; import { Module , AsyncRouteComponentState , AsyncRouteComponentType , Ctx , } from './types' ; /**
 * Returns a new React component, ready to be instantiated.
 * Note the closure here protecting Component, and providing a unique
 * instance of Component to the static implementation of `load`.
 */ export function asyncComponent < Props > ( { loader , Placeholder , chunkName , } ) { let Component = null ; return class AsyncRouteComponent extends React . Component < Props , AsyncRouteComponentState > { /**
         * Static so that you can call load against an uninstantiated version of
         * this component. This should only be called one time outside of the
         * normal render path.
         */ static load ( ) { return loader ( ) . then ( ResolvedComponent => { Component = ResolvedComponent . default || ResolvedComponent ; } ) ; } static getChunkName ( ) { return chunkName ; } static getInitialProps ( ctx ) { if ( Component !== null ) { return Component . getInitialProps ? Component . getInitialProps ( ctx ) : Promise . resolve ( null ) ; } } state = { Component } ; componentDidMount ( ) { AsyncRouteComponent . load ( ) . then ( this . updateState ) ; } updateState = ( ) => { if ( this . state . Component !== Component ) { this . setState ( { Component } ) ; } } ; render ( ) { const { Component : ComponentFromState } = this . state ; if ( ComponentFromState ) { return < ComponentFromState { ... this . props } / > ; } if ( Placeholder ) { return < Placeholder { ... this . props } / > ; } return null ; } } ; }