question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for named imports in React.lazy

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

Currently, React.lazy only accepts an anonymous function that returns a dynamic import object. readLazyComponentType then pulls the default export off of that import object. This does not allow for importing named imports from a module.

The official workaround, according to the React docs:

If the module you want to import uses named exports, you can create an intermediate module that reexports it as the default.

Another, more concise workaround:

const Contacts = React.lazy(() =>
  import('./Contacts.js').then(module => ({ default: module.Contacts }))
);

What is the proposed behavior?

I’d be willing to submit a PR that optionally accepts named imports but defaults to importing the default import. It would look like:

thenable.then(
-  moduleObject => {
+  resolvedComponent => {
    if (lazyComponent._status === Pending) {
-      const defaultExport = moduleObject.default;
+      if (resolvedComponent.default) {
+        resolvedComponent = resolvedComponent.default
+      }
      if (__DEV__) {
-        if (defaultExport === undefined) {
+        if (resolvedComponent === undefined) {
          warning(
            false,
-            'lazy: Expected the result of a dynamic import() call. ' +
+            'lazy: Expected a promise that resolves to a React component. ' +
              'Instead received: %s\n\nYour code should look like: \n  ' +
              "const MyComponent = lazy(() => import('./MyComponent'))",
-            moduleObject,
+            resolvedComponent,
          );
        }
      }
      lazyComponent._status = Resolved;
-      lazyComponent._result = defaultExport;
+      lazyComponent._result = resolvedComponent;
    }
  },
  error => {
    if (lazyComponent._status === Pending) {
      lazyComponent._status = Rejected;
      lazyComponent._result = error;
    }
  },
);

This will also require some updates to the docs.

Let me know your thoughts before I start…

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

react@16.7.0, Chrome 71, macOS 10.14

cc @acdlite @gaearon

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:26
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

95reactions
gaearoncommented, Jan 16, 2019

Thanks but it’s an intentional design decision to not currently allow named imports. We may reconsider it later. You can find the relevant discussion in https://github.com/reactjs/rfcs/pull/64.

50reactions
dailytabscommented, Dec 27, 2020

Seriously? Why not just fix the damn thing! Humans are too content with workarounds. Try to actually fix something and maybe the world won’t be such a pile of trash.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React.lazy without a default export - DEV Community ‍ ‍
The React.lazy function lets you render a dynamic import as a regular component. Before: import OtherComponent from '.
Read more >
Can you deconstruct lazily loaded React components?
React.lazy currently only supports default exports. If the module you want to import uses named exports, you can create an intermediate ...
Read more >
Code-Splitting - React
React.lazy currently only supports default exports. If the module you want to import uses named exports, you can create an intermediate module that...
Read more >
React Lazy Loading Named Exports - CodeSandbox
React Lazy Loading Named Exports. 1. Embed Fork Create Sandbox Sign in. Sandbox Info. React Lazy Loading Named Exports. This is my base...
Read more >
Dynamically Importing Components with React.lazy
Fortunately, React allows us to dynamically import components using its React.lazy API. This function accepts a callback that's expected 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