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

37 lines
864 B
JavaScript
Raw Normal View History

2019-03-28 05:39:06 +07:00
/**
* @prettier
*/
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;
2019-03-28 05:39:06 +07:00
state = { Component: AsyncComponent.Component };
2019-02-03 17:23:33 +07:00
componentDidMount() {
2019-03-28 05:39:06 +07:00
const { Component } = this.state;
2019-02-03 17:23:33 +07:00
if (!Component) {
2019-03-28 05:39:06 +07:00
getComponent()
.then(({ default: Component }) => {
AsyncComponent.Component = Component;
/* eslint react/no-did-mount-set-state:0 */
this.setState({ Component });
})
.catch(err => {
console.error(err);
});
2019-02-03 17:23:33 +07:00
}
}
render() {
2019-03-28 05:39:06 +07:00
const { Component } = this.state;
2019-02-03 17:23:33 +07:00
if (Component) {
2019-03-28 05:39:06 +07:00
// eslint-disable-next-line verdaccio/jsx-spread
2019-02-03 17:23:33 +07:00
return <Component {...this.props} />;
}
return null;
}
};
}