Skip to content

Instantly share code, notes, and snippets.

@conorhastings
Last active March 9, 2017 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conorhastings/7b6b794da035e9c78bdf1432a7edf05a to your computer and use it in GitHub Desktop.
Save conorhastings/7b6b794da035e9c78bdf1432a7edf05a to your computer and use it in GitHub Desktop.
function PromiseComponent(initializeComponent, loadingComponent) {
let cachedComponent;
return class PromiseComponent extends React.Component {
state = { Component: cachedComponent || loadingComponent || () => null };
componentDidMount() {
if (!cachedComponent) {
initializeComponent.then(component => {
cachedComponent = component.default;
this.setState({ Component: cachedComponent });
});
}
}
render() {
const { Component } = this.state;
return <Component {...this.props} />
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment