【问题标题】:React.lazy pattern using Loadable Components使用可加载组件的 React.lazy 模式
【发布时间】: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


【解决方案1】:

我认为解决您的问题的最佳方法是继续使用 React.lazy,就像 recommended by the author of loadable-components 一样,因为“它不再维护,并且与 Webpack v4+ 和 Babel v7+ 不兼容”

【讨论】:

  • 我想你是在提到 react-loadable。我正在使用@loadable/component
猜你喜欢
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 2022-01-15
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
相关资源
最近更新 更多