Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code splitting/react-loadable issue

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.

like image 463
Steven Musumeche Avatar asked Nov 17 '17 17:11

Steven Musumeche


People also ask

Does React support code splitting?

Code splitting is a way to split up your code from a large file into smaller code bundles. These can then be requested on demand or in parallel.

What can you use to handle code splitting in React?

Code-Splitting is a feature supported by bundlers like Webpack, Rollup, and Browserify which can create multiple bundles that can be dynamically loaded at runtime.

Is React loadable maintained?

No, React Loadable should not be used anymore, because it is not being maintained. It used to be the recommended way for lazy loading when rendering on the server side, while React. lazy only works on the client side.

What is loadable in React?

React Loadable is a library by @jamiebuilds that makes it easy to implement code splitting in React and that embraces React's component model. It accomplish its magic using dynamic imports and webpack automatically splits dynamic imports into separate chunks when bundling.


2 Answers

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.

like image 58
Steven Musumeche Avatar answered Oct 13 '22 01:10

Steven Musumeche


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

like image 25
Gustav Avatar answered Oct 13 '22 01:10

Gustav