Last active
September 7, 2017 11:22
-
-
Save id0Sch/bd590d8d340f94b2e35d5c5c5268ab0c to your computer and use it in GitHub Desktop.
AsyncComponent.js
This file contains hidden or 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 React, {Component} from 'react' | |
export default function AsyncComponent(getComponent) { | |
return class AsyncComponent extends Component { | |
static Component = null; | |
state = {Component: AsyncComponent.Component}; | |
componentWillMount() { | |
if (!this.state.Component) { | |
getComponent().then(({default: Component}) => { | |
AsyncComponent.Component = Component | |
this.setState({Component}) | |
}) | |
} | |
} | |
render() { | |
const {Component} = this.state | |
if (Component) { | |
return <Component {...this.props} /> | |
} | |
return null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment