Skip to content

Instantly share code, notes, and snippets.

@kdichev
Created December 9, 2017 20:52
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 kdichev/09b966f12ac100eb481cf33d5e591df7 to your computer and use it in GitHub Desktop.
Save kdichev/09b966f12ac100eb481cf33d5e591df7 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
export default function asyncComponent(importComponent) {
class AsyncComponent extends Component {
constructor(props) {
super(props);
this.state = {
component: null,
};
}
async componentDidMount() {
const { default: component } = await importComponent();
this.setState({
component: component
});
}
render() {
const C = this.state.component;
return C
? <C {...this.props} />
: null;
}
}
return AsyncComponent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment