Skip to content

Instantly share code, notes, and snippets.

@gribnoysup
Created March 1, 2017 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gribnoysup/be4680a5d99e83ef5fe242699ffb9b27 to your computer and use it in GitHub Desktop.
Save gribnoysup/be4680a5d99e83ef5fe242699ffb9b27 to your computer and use it in GitHub Desktop.
import React from 'react'
const { func, bool } = React.PropTypes
export default class AsyncComponent extends React.Component {
static propTypes = {
resolve: func.isRequired,
showProcess: bool
}
static defaultProps = {
showProcess: true
}
state = { component: null }
componentWillMount() {
this.props.resolve((err, content) => {
this.setState({ component: content.default })
})
}
render() {
const { component: Component } = this.state
const { showProcess, ...restProps } = this.props
return Component ? <Component {...restProps} /> : (showProcess && <div>Loading...</div>)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment