Created
March 1, 2017 08:29
-
-
Save gribnoysup/be4680a5d99e83ef5fe242699ffb9b27 to your computer and use it in GitHub Desktop.
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 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