Skip to content

Instantly share code, notes, and snippets.

@notgiorgi
Last active March 22, 2018 01:37
Show Gist options
  • Select an option

  • Save notgiorgi/b8ac02296ea57c33d83846945cd79f1d to your computer and use it in GitHub Desktop.

Select an option

Save notgiorgi/b8ac02296ea57c33d83846945cd79f1d to your computer and use it in GitHub Desktop.
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