为什么react加载异步组件的方法要这么写?原理是什么?

新手上路,请多包涵
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});    
       }  
       render() {      
           const C = this.state.component;     
           return C ? <C {...this.props} /> : null;    
       }
    }  
    return AsyncComponent;
}

在使用某个组件时:
import asyncComponent from './untils/asyncComponent';//异步组件的位置
const Do = asyncComponent(() => import('./pages/Do'));
阅读 984
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题