Skip to content

Instantly share code, notes, and snippets.

@jpgorman
Last active September 28, 2018 09:38
Show Gist options
  • Save jpgorman/352265a3b4c23d19898c208d386bc494 to your computer and use it in GitHub Desktop.
Save jpgorman/352265a3b4c23d19898c208d386bc494 to your computer and use it in GitHub Desktop.
import * as React from "react";
export class LazyLoadModule extends React.Component {
constructor(props) {
super(props);
this.state = {
module: null
};
}
// after the initial render, wait for module to load
async componentDidMount() {
const { resolve } = this.props;
const { default: module } = await resolve();
this.setState({ module });
}
render() {
const { module } = this.state;
if (!module) return <div>Loading module...</div>;
if (module.view) return React.createElement(module.view);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment