Skip to content

Instantly share code, notes, and snippets.

@adityaloshali
Created April 18, 2019 15:38
Show Gist options
  • Save adityaloshali/39e235a57bb994af3b554b5208f69af9 to your computer and use it in GitHub Desktop.
Save adityaloshali/39e235a57bb994af3b554b5208f69af9 to your computer and use it in GitHub Desktop.
Dynamic Import Component
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