Last active
October 7, 2018 03:46
-
-
Save theKashey/bd9e699492e4bcb68ff31f5918a1c9dc 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
const loadable = (loaderFunction) => | |
class AsyncComponent extends React.Component { | |
state = { ResultComponent: null, error: false, }; | |
componentWillMount() { | |
loaderFunction | |
.then(result => this.setState({ ResultComponent: result.default || result})) // "es6" default export | |
.catch(() => this.setState({ error: true }); | |
} | |
render() { | |
const { error, ResultComponent } = this.state; | |
// Display loaded component | |
return ResultComponent | |
? <ResultComponent {...this.props}/> | |
: (error ? <ErrorComponent /> : <LoadingComponent />) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment