Created
January 18, 2018 08:54
-
-
Save nitish24p/5c82f4ad13358e37de2905b55116208c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as RoutingConstants from './constants/RoutingConstants'; | |
import { Route, Switch } from 'react-router-dom'; | |
import LoadingComponent from './pages/LoadingComponent'; | |
import React, { Component } from 'react'; | |
function asyncComponent(importComponent: Function, LoadingComponent: Function) { | |
class AsyncComponent extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
component: null | |
}; | |
} | |
async componentDidMount() { | |
const { default: component } = await importComponent(); | |
this.setState({ | |
component: component | |
}); | |
} | |
render() { | |
const C = this.state.component; | |
return C ? <C {...this.props} /> : <LoadingComponent />; | |
} | |
} | |
return AsyncComponent; | |
} | |
const Products = asyncComponent(() => import('./pages/Products'), LoadingComponent); | |
const Homepage = asyncComponent(() => import('./pages/Homepage'), LoadingComponent); | |
const AboutUs = asyncComponent(() => import('./pages/AboutUs'), LoadingComponent); | |
class App extends Component<{}, {}> { | |
render() { | |
return ( | |
<div> | |
<Switch> | |
<Route exact path={'/'} component={Homepage} /> | |
<Route exact path={'/products'} component={Products} /> | |
<Route exact path={'/about-us'} component={AboutUs} /> | |
</Switch> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment