Created
June 26, 2017 07:39
-
-
Save notgiorgi/b159ada5bef020b3b5cc1689405c069e to your computer and use it in GitHub Desktop.
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
| export const withLazyLoading = (getComponent, { LoaderComponent = null }) => | |
| class WithLazyLoading extends React.Component { | |
| state = { | |
| Component: null, | |
| } | |
| componentWillMount() { | |
| const { startLoading, stopLoading } = this.props | |
| startLoading() | |
| getComponent() | |
| .then(({ default: component }) => { | |
| stopLoading() | |
| this.setState({ Component: component }) | |
| }) | |
| .catch(err => { | |
| stopLoading(err) | |
| }) | |
| } | |
| render() { | |
| const { Component } = this.state | |
| const { onStartLoading, onFinishLoading, ...rest } = this.props | |
| return Component === null | |
| ? <LoaderComponent /> | |
| : <Component {...rest} /> | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment