Skip to content

Instantly share code, notes, and snippets.

@codeaid
Last active June 1, 2017 10:40
Show Gist options
  • Save codeaid/ced595328c9b36b5e8df6313ca90081b to your computer and use it in GitHub Desktop.
Save codeaid/ced595328c9b36b5e8df6313ca90081b to your computer and use it in GitHub Desktop.
AsyncComponent.jsx
import * as React from 'react';
import {IAsyncComponentState} from 'types';
export function asyncComponentLoader(callback: Function) {
return class extends React.Component<{}, IAsyncComponentState> {
// initial component state
public state: IAsyncComponentState = {
component: null,
};
/**
* Invoked immediately after a component is mounted
*
* @return {void}
*/
async componentDidMount() {
const {default: component} = await callback();
this.setState({
component,
});
}
/**
* Render current component
*
* @return {JSX.Element}
*/
render() {
const {component: Component} = this.state;
return Component
? <Component {...this.props} />
: null;
}
};
}
export default asyncComponentLoader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment