Created
July 24, 2018 11:16
-
-
Save matheus1lva/fa6f9ad5efd7cbba5b6b82769a119622 to your computer and use it in GitHub Desktop.
Async loading
This file contains hidden or 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 React from 'react'; | |
import PropTypes from 'prop-types'; | |
import Loading from './components/Loading'; | |
class AsyncRoute extends React.PureComponent { | |
constructor(props) { | |
super(props); | |
this.state = { | |
loaded: false | |
}; | |
} | |
componentDidMount() { | |
this.props.loading.then((module) => { | |
this.component = module.default; | |
this.setState({ | |
loaded: true | |
}); | |
}); | |
} | |
render() { | |
if (this.state.loaded) { | |
return <this.component {...this.props.routeProps} />; | |
} | |
return <Loading />; | |
} | |
} | |
AsyncRoute.propTypes = { | |
routeProps: PropTypes.object, | |
loading: PropTypes.any.isRequired | |
}; | |
AsyncRoute.defaultProps = { | |
routeProps: {} | |
}; | |
if (module.hot) { | |
module.hot.accept(); | |
} | |
export default AsyncRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment