1 | import React, { Component } from 'react';
|
2 |
|
3 | export default function asyncComponent(importComponent) {
|
4 | class AsyncComponent extends Component {
|
5 | constructor(props) {
|
6 | super(props);
|
7 |
|
8 | this.state = {
|
9 | component: null,
|
10 | };
|
11 | }
|
12 |
|
13 | async componentDidMount() {
|
14 | const { default: component } = await importComponent();
|
15 |
|
16 | this.setState({
|
17 | component,
|
18 | });
|
19 | }
|
20 |
|
21 | render() {
|
22 | const C = this.state.component;
|
23 |
|
24 | return C ? <C {...this.props} /> : null;
|
25 | }
|
26 | }
|
27 |
|
28 | return AsyncComponent;
|
29 | }
|
30 |
|
\ | No newline at end of file |