-
-
Save codeaid/ced595328c9b36b5e8df6313ca90081b to your computer and use it in GitHub Desktop.
AsyncComponent.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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