Created
December 3, 2021 10:56
-
-
Save lusan/f7cd09bb7d655a17eade3965ed232dbb to your computer and use it in GitHub Desktop.
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'; | |
class AsyncComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
isLoading: true, | |
component: null, | |
error: null | |
} | |
} | |
componentWillMount() { | |
const { importModule } = this.props; | |
importModule() | |
.then((module) => { | |
const component = module.default; | |
console.log(component) | |
this.setState(({ | |
component, | |
isLoading: false | |
})) | |
}) | |
.catch(err => { | |
console.log(error) | |
this.setState({ | |
isLoading: false, | |
error | |
}) | |
}) | |
} | |
render() { | |
const { isLoading, component, error } = this.state; | |
if(isLoading) { | |
return <div>Loading</div> | |
} | |
if(error) { | |
return <div>{error}</div> | |
} | |
return <div>{component}</div> | |
} | |
} | |
const A = () => { | |
return ( | |
<div> | |
<AsyncComponent importModule={() => import('./F')} /> | |
</div> | |
) | |
} | |
// We keep a reference to prevent uglify remove | |
export default A |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment