Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created October 20, 2017 18:24
Show Gist options
  • Save tkssharma/025def939edf397bbdf15fd39016258a to your computer and use it in GitHub Desktop.
Save tkssharma/025def939edf397bbdf15fd39016258a to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
export default function asyncComponent(getComponent) {
class AsyncComponent extends Component {
static Component = null;
state = { Component: AsyncComponent.Component };
componentWillMount() {
if (!this.state.Component) {
getComponent().then(Component => {
AsyncComponent.Component = Component
this.setState({ Component })
})
}
}
render() {
const { Component } = this.state
if (Component) {
return <Component {...this.props} />
}
return null
}
}
return AsyncComponent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment