Last active
March 9, 2017 15:46
-
-
Save conorhastings/7b6b794da035e9c78bdf1432a7edf05a 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
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