1
0
Fork 1
mirror of https://github.com/SomboChea/ui synced 2024-06-29 22:46:22 +07:00
verdaccio-ui/src/utils/asyncComponent.js

29 lines
686 B
JavaScript
Raw Normal View History

2019-02-03 17:23:33 +07:00
import React from 'react';
export function asyncComponent(getComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = {Component: AsyncComponent.Component};
componentDidMount() {
const {Component} = this.state;
if (!Component) {
getComponent().then(({default: Component}) => {
AsyncComponent.Component = Component;
/* eslint react/no-did-mount-set-state:0 */
this.setState({Component});
});
}
}
render() {
const {Component} = this.state;
if (Component) {
return <Component {...this.props} />;
}
return null;
}
};
}