Last active
March 22, 2018 01:37
-
-
Save notgiorgi/b8ac02296ea57c33d83846945cd79f1d 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
| function withLazyLoading( | |
| getComponent, | |
| Spinner = null, | |
| onError = noop, | |
| ) { | |
| return class LazyLoadingWrapper extends React.Component { | |
| state = { | |
| Component: null, | |
| } | |
| componentWillMount() { | |
| const { onLoadingStart, onLoadingEnd, onError } = this.props | |
| onLoadingStart() | |
| // Before the wrapper component mounts we fire importer function | |
| getComponent() | |
| .then(esModule => { | |
| // and store the component in state | |
| this.setState({ Component: esModule.default }) | |
| }) | |
| .catch(err => { | |
| onError(err, this.props) | |
| }) | |
| } | |
| render() { | |
| const { Component } = this.state | |
| if (!Component) return Spinner | |
| return <Component {...this.props} /> | |
| } | |
| } | |
| } | |
| function noop() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment