Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named imports in lazy feature #14390

Closed
barth12 opened this issue Dec 4, 2018 · 5 comments
Closed

Named imports in lazy feature #14390

barth12 opened this issue Dec 4, 2018 · 5 comments

Comments

@barth12
Copy link

barth12 commented Dec 4, 2018

Hi guys,

How could I import named imports using lazy syntax?

Before we could use

import { MainComponent, Component1, Component2 } from './components';

How to achieve something like this (although this does not work)?:

const { MainComponent, Component1, Component2 } = React.lazy(() => import('./components'));

Thank you and have a good day :-)

@brookslybrand
Copy link

Looks like it's not yet available

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

I think that's the only solution for now

@TrySound
Copy link
Contributor

TrySound commented Dec 5, 2018

This is how I do it.

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

@barth12
Copy link
Author

barth12 commented Dec 8, 2018

@TrySound Thank you for that. Still we need to import every component separately though.

@aweary
Copy link
Contributor

aweary commented Dec 10, 2018

@barth12 yeah, that's the one big caveat with how React.lazy works with named exports right now. It expects that you're importing exactly one component. You won't currently be able to use destructuring like you want, they'll have to be separate React.lazy calls as @TrySound illustrated.

Closing since this is a documented shortcoming and expected behavior for now.

@amitesh786
Copy link

I try with this ->

const myComponent = React.lazy(() => import('../MyComponent'), 'default');

It works fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants