简体   繁体   English

代码拆分/反应可加载问题

[英]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.

相关问题 使用Loadable从react-loadable拆分代码会导致屏幕闪烁 - code splitting with Loadable from react-loadable causes screen flicker 使用create-react-app和react-loadable进行代码拆分不起作用 - Code splitting with create-react-app and react-loadable is not working Webpack-通过webpack代码拆分和react-loadable防止文件重复 - Webpack - Prevent file duplication with webpack code splitting & react-loadable 使用react-loadable进行代码拆分会产生错误:找不到模块“。” - Code Splitting with react-loadable gives Error : Cannot find module “.” 代码拆分与react-loadable会降低性能吗? - Code splitting with react-loadable slow down performance? 我应该使用 react-loadable 还是 loadable-components 进行代码拆分? - Should I use react-loadable or loadable-components for code splitting? react-loadable 块已加载但代码未执行 - react-loadable Chunks loaded but code not executed 添加React.lazy或react-loadable并进行动态导入(代码拆分)会导致我的某些功能中断 - Adding React.lazy or react-loadable and doing dynamic import (Code splitting) causes some of my functionalities to breaks 从react-loadable随附的组件导入外部模块时出现问题 - Issue importing external modules from component included with react-loadable 暂时禁用 react-loadable - Temporarily disable react-loadable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM