【发布时间】:2020-06-19 07:49:19
【问题描述】:
我有以下情况,我事先不知道要加载哪个组件。使用React.lazy的解决方案如下
import React, { lazy, Suspense } from "react";
export default class CallingLazyComponents extends React.Component {
render() {
var ComponentToLazyLoad = null;
if(this.props.name == "A") {
ComponentToLazyLoad = lazy(() => import("./AComponent"));
} else if(this.props.name == "B") {
ComponentToLazyLoad = lazy(() => import("./BComponent"));
}
return (
<div>
<h1>This is the Base User: {this.state.name}</h1>
<Suspense fallback={<div>Loading...</div>}>
<ComponentToLazyLoad />
</Suspense>
</div>
)
}
}
我怎样才能使用Loadable Components 达到同样的效果
【问题讨论】:
标签: reactjs code-splitting loadable-component