Created
April 18, 2019 15:38
-
-
Save adityaloshali/39e235a57bb994af3b554b5208f69af9 to your computer and use it in GitHub Desktop.
Dynamic Import Component
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, { Component } from 'react' | |
import { SyncLoader } from 'react-spinners' | |
const lazyComponent = (importComponent) => class extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
component: null | |
} | |
} | |
componentDidMount() { | |
importComponent() | |
.then((cmp) => this.setState({ component: cmp.default })) | |
} | |
render() { | |
const Lazy = this.state.component | |
const loaderStyles = { | |
display: 'flex', | |
justifyContent: 'center', | |
alignItems: 'center', | |
width: '100%', | |
height: '100%' | |
} | |
return Lazy ? | |
<Lazy {...this.props} /> | |
: | |
<div style={loaderStyles}> | |
<SyncLoader | |
sizeUnit="px" | |
size={10} | |
color="#2065bf" | |
loading | |
/> | |
</div> | |
} | |
} | |
export default lazyComponent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment