Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matheus1lva/fa6f9ad5efd7cbba5b6b82769a119622 to your computer and use it in GitHub Desktop.
Save matheus1lva/fa6f9ad5efd7cbba5b6b82769a119622 to your computer and use it in GitHub Desktop.
Async loading
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