简体   繁体   English

使用组件作为属性的react-loadable导入动态对象

[英]react-loadable import dynamicaly object with components as properties

I have an index file where I export all of my SVG components: 我有一个索引文件,可在其中导出所有SVG组件:

import Twitter from './Twitter';
import Upload from './Upload';
import Cloud from './Cloud';
import Share from './Share';
import Eye from './Eye';
import Trash from './Trash';
import Search from './Search';

export {
  Facebook,
  Twitter,
  Upload,
  Cloud,
  Share,
  Eye,
  Trash,
  Search,
};

Then later in the app, I use them like: 然后稍后在应用程序中,我将它们像这样使用:

import * as Icons from './index.js';

<Icons.Twitter />
...
<Icons.Search />

because I have a lot of these icons I would like to load them asynchronously with react-lodable . 因为我有很多这些图标,所以我想使用react-lodable异步加载它们。

I tried: 我试过了:

import React from 'react';
import Loadable from 'react-loadable';
import PulsingCircleLoader from '../PulsingCircleLoader';

const Icons = Loadable({
  loader: () => import(/* webpackChunkName: "icons" */ './index'),
  loading: () => <PulsingCircleLoader isLoading />,
});

export default Icons;

But this gives me an error: 但这给我一个错误:

warning.js?6327:33 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. warning.js?6327:33警告:React.createElement:类型无效-预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports 您可能忘记了从定义文件中导出组件,或者您可能混淆了默认导入和命名导入

that's probably because Icons.Twitter is at some point an PulsingCircleLoader with no static Twitter property. 那可能是因为Icons.Twitter在某种程度上是一个没有静态Twitter属性的PulsingCircleLoader

How can I get around this? 我该如何解决?

This happens because you are importing your icons with export { ... } statement. 发生这种情况是因为您正在使用export { ... }语句导入图标。 But when you are using call like this import(/* webpackChunkName: "icons" */ './index'), the ./index file must have export default statement 但是,当您使用像import(/* webpackChunkName: "icons" */ './index'),./index import(/* webpackChunkName: "icons" */ './index'), ./index文件必须具有export default语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM