react异步引用组件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1036548849/article/details/85248102
import React, { Component } from "react";

export default function asyncComponent(importComponent) {
  class AsyncComponent extends Component {
    constructor(props) {
      super(props);

      this.state = {
        component: null
      };
    }

    async componentDidMount() {
      const { default: component } = await importComponent();

      this.setState({
        component: component
      });
    }

    render() {
      const C = this.state.component;

      return C ? <C {...this.props} /> : null;
    }
  }

  return AsyncComponent;
}

react参考文档

猜你喜欢

转载自blog.csdn.net/qq1036548849/article/details/85248102