Skip to content

Instantly share code, notes, and snippets.

@Spyna
Created May 4, 2018 15:43
Show Gist options
  • Save Spyna/368c5149377aa5fe0bc8892d69d19616 to your computer and use it in GitHub Desktop.
Save Spyna/368c5149377aa5fe0bc8892d69d19616 to your computer and use it in GitHub Desktop.
import React from 'react';
import LoadingOverlay from './Loader'
export default (loader, props) => (
class AsyncComponent extends React.Component {
constructor(props) {
super(props);
this.Component = null;
this.state = { Component: AsyncComponent.Component };
}
componentWillMount() {
if (!this.state.Component) {
loader().then((Component) => {
AsyncComponent.Component = Component;
this.setState({ Component });
});
}
}
render() {
if (this.state.Component) {
return (
<this.state.Component { ...this.props } { ...props } />
)
} else {
return <LoadingOverlay />;
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment