React Lazy - Element type is invalid. Received a promise that resolves to: [object Promise]. Lazy element type must resolve to a class or function.

See original GitHub issue

I am trying to code split with React lazy and get error:

Element type is invalid. Received a promise that resolves to: [object Promise]. Lazy element type must resolve to a class or function.

import React, { lazy, Suspense } from 'react'
import * as ReactDOM from 'react-dom'

const Foo = React.lazy(() => import('./foo/foo'))

const App = () => {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <Foo />
      </Suspense>
    </div>
  )
}

repo: https://github.com/jcmoore0/react-lazy-fuse/tree/master/src

Is this method supported? Fusebox V4 is fantastic. Thanks!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

11reactions
Krak86commented, Jul 30, 2020

According to React documentation route-based-code-splitting:

React.lazy currently only supports default exports. If the module you want to import uses named exports, you can create an intermediate module that reexports it as the default. This ensures that tree shaking keeps working and that you don’t pull in unused components.

So you should do the next steps, for not default modules

// ManyComponents.js
export const MyComponent = /* ... */;
export const MyUnusedComponent = /* ... */;
// MyComponent.js
export { MyComponent as default } from "./ManyComponents.js";
// MyApp.js
import React, { lazy } from 'react';
const MyComponent = lazy(() => import("./MyComponent.js"));

For me it helps.

6reactions
nchangedcommented, Sep 25, 2019

@jcmoore0 thanks 😉 I think it clearly states the error.

Received a promise that resolves to: [object Promise]. Lazy element type must resolve to a class or function.

const Foo = React.lazy( async () => (await import('./foo/foo').MyComponent )

or default as you wish. I am closing this issue since it’s obvious. You can also try react-universal example

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Received a promise that resolves to: undefined ...
Element type is invalid. Received a promise that resolves to: undefined. Promise elements must resolve to a class or function. This is SibaCard ......
Read more >
element type is invalid. received a promise that resolves to
Received a promise that resolves to: [object Promise]. Lazy element type must resolve to a class or function. import React, { lazy, Suspense...
Read more >
(React) Element type is invalid, expected a string (for built in ...
To solve the error "Element type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got",...
Read more >
Lazy Element Type Must Resolve To A Class Or Function (In ...
Received a promise that resolves to: [object Promise]. Lazy element type must resolve to a class or function. import React, { lazy, Suspense...
Read more >
lazy - React Docs
You need to return a Promise or some other thenable (a Promise-like object with a then method). It needs to eventually resolve to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found