Skip to content

Instantly share code, notes, and snippets.

@theKashey
Last active October 7, 2018 03: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 theKashey/bd9e699492e4bcb68ff31f5918a1c9dc to your computer and use it in GitHub Desktop.
Save theKashey/bd9e699492e4bcb68ff31f5918a1c9dc to your computer and use it in GitHub Desktop.
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