Skip to content

Instantly share code, notes, and snippets.

@gaganjakhotiya
Created October 27, 2017 14:08
Show Gist options
  • Save gaganjakhotiya/4315362fdfd94e0f30ca68a09a4133b1 to your computer and use it in GitHub Desktop.
Save gaganjakhotiya/4315362fdfd94e0f30ca68a09a4133b1 to your computer and use it in GitHub Desktop.
Use Lazy Widget
import React from 'react'
import widgetMap from './widgetMap'
function getLazyComponent(widgetName, defaultProps, loader = 'loading...') {
if (!widgetMap[widgetName])
throw 'WidgetNotFound: Provided widget name does not exist.'
return (
class LoaderWrapper extends React.Component {
static defaultProps = defaultProps
constructor(props) {
super(props)
this.state = {
ComponentClass: null
}
}
componentDidMount() {
widgetMap[widgetName](
ComponentModule => !this._calledComponentWillUnmount && this.setState({
ComponentClass: ComponentModule.default || ComponentModule
})
)
}
render() {
const { ComponentClass } = this.state
return ComponentClass
? React.createElement(ComponentClass, this.props)
: loader
}
}
)
}
export default getLazyComponent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment