Code splitting/react-loadable issue
Asked Answered
V

4

9

I'm trying to introduce code splitting into my app using 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.

Veto answered 17/11, 2017 at 17:2 Comment(0)
V
24

Turns out that you need to pass a component to the loading option and not JSX. The documentation clearly says this, I just missed it.

Veto answered 17/11, 2017 at 17:20 Comment(2)
I implemented code splitting (react-loadable) with the help of this tutorial youtu.be/AR5GSZuox1kPriestly
For more detail: loading: () => <div>loading</div>Mule
Q
3

Make sure to use default exports since when you import it's not using named exports: loader: () => import(/* webpackChunkName: "home" */ './Home')

Quaternary answered 18/3, 2019 at 18:47 Comment(0)
K
2

Don't pass jsx to loading key to Loadable component, provide valid react component.

const LoadableComponent = Loadable({
    loader: () => import('components/Shared/Logo/Logo'),
    loading: () => <div>loading</div>, // pass component, not jsx
});
Kistler answered 7/10, 2018 at 18:25 Comment(0)
P
1

For the guys who got here because they're server side rendering app (server babel transpiled files) spitting out the error above, it may happen because you are using airbnb babel-plugin-dynamic-import-node without setting noInterop to false on .babelrc as below: { "plugins": [ ["dynamic-import-node", { "noInterop": true }] ] }


Predict answered 15/12, 2018 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.