Skip to content

Instantly share code, notes, and snippets.

@notgiorgi
Created June 26, 2017 07:39
Show Gist options
  • Select an option

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

Select an option

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