【问题标题】:does 'react-loadable' excepts exact path to the component?'react-loadable' 除了组件的确切路径吗?
【发布时间】:2023-04-02 09:35:01
【问题描述】:

我在我的应用程序中使用“react-loadable”来延迟加载一些我不想在应用程序加载后立即呈现的组件,目前我正在遵循基于路由的代码拆分。

我的文件结构:

ignitus-About/Components/index.js的内容如下:

export { default as About } from './About';

这是我的懒加载 AboutUs 组件的代码 sn-p:

const AboutUs = Loadable({
  loader: () => import('../ignitus-About/Components/About'),
  loading: Loading,
});

但您会注意到,我正在编写 About 组件的确切/完整路径,但在我的 Components 目录中,我只有 2 个文件,一个是 index.js,另一个是 About.js

这里index.js 正在通过这样做导出关于 组件:

export { default as About } from './About';

但是当我在我的可加载组件中写这个时:

 const AboutUs = Loadable({
      loader: () => import('../ignitus-About/Components'),
      loading: Loading,
    });

它会抛出一个错误,所以我的问题是 react-lodable 是否期望组件的确切路径,如果不是,那么我如何从可加载组件中的 index.js 导出我的 About 组件。

entire codebase

所以,当我像这样延迟加载我的组件时:

 const AboutUs = Loadable({
      loader: () => import('../ignitus-About/Components/About'),
      loading: Loading,
    });

效果很好。

如果我尝试像这样延迟加载:

 const AboutUs = Loadable({
      loader: () => import('../ignitus-About/Components'),
      loading: Loading,
    });

它抛出一个错误:

index.js:1452 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

    Check the render method of `LoadableComponent`.
        in LoadableComponent (created by Route)
        in Route (at publicRoutes.js:56)
        in Switch (at publicRoutes.js:41)
        in div (at publicRoutes.js:39)
        in PublicRoutes (created by Route)
        in Route (at App.js:36)
        in Switch (at App.js:34)
        in div (at App.js:25)
        in App (at src/index.js:29)
        in Router (created by BrowserRouter)
        in BrowserRouter (at src/index.js:28)
        in Provider (at src/index.js:27)

【问题讨论】:

  • 你能详细说明你的代码是如何“抛出和错误”的吗?你期待什么,实际发生了什么?如果您遇到异常/错误,请发布它发生的行和异常/错误详细信息。请edit这些详细信息,否则我们可能无法提供帮助。
  • @FrankerZ 我已经用错误和更多细节更新了问题。

标签: reactjs react-router lazy-loading react-loadable


【解决方案1】:

问题在于您的export { default as About } from './About'; 声明。你没有导出默认值,所以当你在这里使用你的语句时,它没有看到默认值:

loader: () => import('../ignitus-About/Components'),

当可加载组件尝试导入您的组件并使用 react 对其进行初始化时,它会尝试从 ../ignitus-About/Components 的默认导出中执行此操作。这不存在。加载程序应该如何知道您想要 About,而不是从这个导入中说出 Blog 组件?

无论您的导入是可加载的,它都必须是一个单个组件。如果您有多个组件,则需要使用多个 react-loadable。

【讨论】:

  • 那么,如果我必须让它工作,那么我要写什么?
  • loader: () => import('../ignitus-About/Components/About'),
  • 这就是我在问题中提到的,除了 ``` loader: () => import('../ignitus-About/Components/ 之外,还有什么方法可以更改 index.js关于'), ```这样导出。
  • @Div 你为什么还要尝试创建另一个导出层?为什么关于我们中有一个组件文件夹?如果是这种情况,您的可加载组件如何知道要加载哪个组件,如果您有多个?
  • 你刚才说你不愿意将多个组件放在组件文件夹中,那为什么还要以文件夹开头呢?为什么不简单地将每个主要组件放在 ignitus-About 下的 index.js 文件中?查看here,了解您应该如何构建子文件夹。容器子文件夹没有理由。
猜你喜欢
  • 2019-04-03
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 2021-07-06
  • 1970-01-01
  • 2018-09-03
相关资源
最近更新 更多