[英]Code splitting/react-loadable issue
I'm trying to introduce code splitting into my app using react-loadable. 我正在尝试使用react-loadable将代码拆分到我的应用程序中。 I tried it on a very simple component:
我在一个非常简单的组件上尝试了它:
const LoadableComponent = Loadable({
loader: () => import('components/Shared/Logo/Logo'),
loading: <div>loading</div>,
});
However, when this component is rendered, I get the following error: 但是,当呈现此组件时,我收到以下错误:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of `LoadableComponent`.
in LoadableComponent (created by AppHeader)
in div (created by AppHeader)
in AppHeader (created by PlainChatApp)
in div (created by PlainChatApp)
in PlainChatApp (created by DragDropContext(PlainChatApp))
in DragDropContext(PlainChatApp) (created by Connect(DragDropContext(PlainChatApp)))
in Connect(DragDropContext(PlainChatApp))
in Provider
in AppContainer
in ErrorBoundary
The above error occurred in the <LoadableComponent> component:
in LoadableComponent (created by AppHeader)
in div (created by AppHeader)
in AppHeader (created by PlainChatApp)
in div (created by PlainChatApp)
in PlainChatApp (created by DragDropContext(PlainChatApp))
in DragDropContext(PlainChatApp) (created by Connect(DragDropContext(PlainChatApp)))
in Connect(DragDropContext(PlainChatApp))
in Provider
in AppContainer
in ErrorBoundary
I don't see anything obvious that I'm doing wrong, and I'm unable to file an issue in that repo. 我没有看到任何明显的错误,我也无法在该回购中提出问题。
Turns out that you need to pass a component to the loading
option and not JSX. 事实证明,您需要将组件传递给
loading
选项而不是JSX。 The documentation clearly says this, I just missed it. 文档清楚地说明了这一点,我只是错过了它。
Don't pass jsx to loading key to Loadable component, provide valid react component. 不要将jsx传递给Loadable组件加载密钥,提供有效的反应组件。
const LoadableComponent = Loadable({
loader: () => import('components/Shared/Logo/Logo'),
loading: () => <div>loading</div>, // pass component, not jsx
});
对于那些来到这里的人,因为他们是服务器端渲染应用程序(服务器babel转换文件)吐出上面的错误,可能会发生因为你使用airbnb babel-plugin-dynamic-import-node而没有将noInterop
设置为false .babelrc
如下: { "plugins": [ ["dynamic-import-node", { "noInterop": true }] ] }
确保使用default exports
因为导入时不使用命名导出: loader: () => import(/* webpackChunkName: "home" */ './Home')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.