2019-02-03 17:23:33 +07:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
export function asyncComponent(getComponent) {
|
|
|
|
return class AsyncComponent extends React.Component {
|
2019-06-25 04:03:21 +07:00
|
|
|
public static Component = null;
|
|
|
|
public state = { Component: AsyncComponent.Component };
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-25 04:03:21 +07:00
|
|
|
public 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
|
|
|
}
|
|
|
|
}
|
2019-06-25 04:03:21 +07:00
|
|
|
|
|
|
|
public 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-06-20 19:37:28 +07:00
|
|
|
// @ts-ignore
|
2019-02-03 17:23:33 +07:00
|
|
|
return <Component {...this.props} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|