2019-06-25 05:01:13 +07:00
|
|
|
import React, { ComponentClass } from 'react';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-25 05:01:13 +07:00
|
|
|
export function asyncComponent(getComponent): ComponentClass {
|
2019-02-03 17:23:33 +07:00
|
|
|
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 05:01:13 +07:00
|
|
|
public componentDidMount(): void {
|
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
|
|
|
|
2019-06-25 05:01:13 +07:00
|
|
|
public render(): JSX.Element | null {
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|