Created
October 27, 2017 14:08
-
-
Save gaganjakhotiya/4315362fdfd94e0f30ca68a09a4133b1 to your computer and use it in GitHub Desktop.
Use Lazy Widget
This file contains 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
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