简体   繁体   English

从react-loadable随附的组件导入外部模块时出现问题

[英]Issue importing external modules from component included with react-loadable

I've a component loaded using react-loadable (5.4.0) that also loads some other local and external libraries: 我已经使用react-loadable(5.4.0)加载了一个组件,该组件还加载了一些其他本地和外部库:

import { validationAPI, docstringAPI, autocompleteAPI } from 'utils/request';
import something from './something';

import CodeMirror from 'react-codemirror';
import CM from 'codemirror';

import 'codemirror-docs-addon';
import 'codemirror-docs-addon/lib/main.css';

class MyComponent extends React.Component {
  render() {
     return (
        <div>What I do here it doesn't matter</div>
     )
  }
}
export default MyComponent

And this is the defined loadable: 这是定义的可加载项:

import React from 'react';
import Loadable from 'react-loadable';

const Loading = () => (
  <div>
    Loading stuff
  </div>
);

const LoadableComponent = Loadable({
  loader: () => import('./MyComponent'),
  loading: Loading,
});

export default (props) => (
  <LoadableComponent {...props} />
);

The issue I have: if the module I include asynchronously also imports local modules never imported before from my app, I get this error: 我遇到的问题:如果我异步包含的模块还导入了从未从我的应用程序导入过的本地模块,则会出现此错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. 警告:React.createElement:类型无效-预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到了:对象。

Check the render method of LoadableComponent . 检查LoadableComponent的渲染方法。 in LoadableComponent ... 在LoadableComponent ...

For example: in the code above I have this ./something imported. 例如:在上面的代码中,我已经导入了./something If this module has never been loaded before in my app I get the error above. 如果从未在我的应用程序中加载过此模块,则会收到上述错误。

If I: 如果我:

  • import the module somewhere else (as for validationAPI , which is imported with no issues) 将该模块导入其他地方(如validationAPI ,导入没有问题)
  • remove the import 删除导入

...the app works and I've the async behavior. ...该应用程序正常运行,并且我具有异步行为。

Note : If the imported stuff is used or not doesn't matters. 注意 :是否使用导入的东西无关紧要。

I also tried to move the something module somewhere else, but this seems not related to the module position or to relative import syntax. 我还尝试将something模块移动到其他位置,但这似乎与模块位置或相对导入语法无关。

Note also that externals libraries does not give me any issues: for example: the codemirror library is included only in that file, but it still works. 还要注意,外部库不会给我带来任何问题:例如: codemirror库仅包含在该文件中,但仍然可以使用。 Only local modules give me the issue. 只有本地模块给我这个问题。

The only workaround found: not import anything never imported before, but include the something module content inline in the component's file. 找到的唯一解决方法:不导入以前从未导入的任何内容,而是在组件文件中内联something内容模块。

This is acceptable but bloat the file size a bit. 这是可以接受的,但是文件大小有点膨胀。

I'm not sure this is an issue with react-loadable or webpack or something else. 我不确定这是react-loadable或webpack还是其他问题。 Any suggestion? 有什么建议吗?

I'm running into the same issue as you... Did you ever figure it out? 我遇到了与您相同的问题...您是否解决过?

I was able to get it to work with Loadable.Map(). 我能够使它与Loadable.Map()一起使用。 But I feel I shouldn't even have to do it this way! 但是我觉得我什至不必这样做!

https://github.com/jamiebuilds/react-loadable#loading-multiple-resources https://github.com/jamiebuilds/react-loadable#loading-multiple-resources

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

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