What are some use cases for using named imports with the import keyword (to import a default export)?

Question

What are some use cases for using named imports with the import keyword (to import a default export)?

Answer

We can combine named imports with the import statement (to import a default export) for the same reasons we would combine named exports and default exports.

Generally, this includes importing the expected export of a module, the default export, importing any utility exports that may not be necessary but could come in handy, the named exports, and/or importing pieces of a module that are the internals of a private variable without exposing the entire private variable (again these would be named exports).

5 Likes

Sounds easier and cleaner to put export before any thing we want to, like:

export let flightRequirements = { requiredStaff: 4, requiredSpeedRange: 700 };

And just using at the beginning:
import {flightRequirements, etc} from './airplane'

or even for tiny files import * from ā€˜./airplane.jsā€™

4 Likes

yeah Iā€™m at least glad they showed us multiple ways, this is way easier though

1 Like

I like this way too, as itā€™s easier to use it in the new file. Otherwise, I have to go back to the old file to check, what was the name of the object, with import * as ā€¦ I can just choose a new name.

So would this be a good place to slot in this thought: Iā€™m worried about the Alias function (not Jennifer Garner ) if you get too cutesy can you imagine a night mare of flags on the play because aliases got forgotten and thatā€™s the real mistake the error message was trying to get you to fix the WHOLE time? is this a nightmare scenario or plausible concern?

2 Likes

Never import things you donā€™t need. This will just make your application larger and slower

This scenario makes total sense. I think is an important matter to be aware and careful about if we decide to follow the ā€œaliasā€ path.
Maybe the ā€œalias nightmareā€ can be related with the ā€œcallback ā– ā– ā– ā– ā€ (?).