Last active
May 30, 2017 15:43
-
-
Save tkh44/eab3a6d988cefdc62032dd382b8c67e5 to your computer and use it in GitHub Desktop.
modified versions of @acdlite's async component. Uses async/await.
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 { Component, createElement } from 'react' | |
export default function asyncComponent (getComponent) { | |
return class AsyncComponent extends Component { | |
static NextComponent = null; | |
state = { NextComponent: AsyncComponent.NextComponent } | |
componentWillMount = async () => { | |
if (this.state.NextComponent) { | |
return | |
} | |
try { | |
const NextComponent = await getComponent() | |
AsyncComponent.NextComponent = NextComponent | |
this.setState({ NextComponent }) | |
} catch (err) { | |
console.error(err) | |
} | |
} | |
render () { | |
const { NextComponent } = this.state | |
if (NextComponent) { | |
return createElement(NextComponent, { ...this.props }) | |
} | |
return null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment