Skip to content

Instantly share code, notes, and snippets.

@tkh44
Last active May 30, 2017 15:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkh44/eab3a6d988cefdc62032dd382b8c67e5 to your computer and use it in GitHub Desktop.
Save tkh44/eab3a6d988cefdc62032dd382b8c67e5 to your computer and use it in GitHub Desktop.
modified versions of @acdlite's async component. Uses async/await.
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