Skip to content

Instantly share code, notes, and snippets.

@lusan
Created December 3, 2021 10:56
Show Gist options
  • Save lusan/f7cd09bb7d655a17eade3965ed232dbb to your computer and use it in GitHub Desktop.
Save lusan/f7cd09bb7d655a17eade3965ed232dbb to your computer and use it in GitHub Desktop.
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