Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Import Both Default and Named Exports
chantastic
chantastic

Posted on • Originally published atchan.dev on

     

Import Both Default and Named Exports

We can mix and match import styles to keep code tidy and direct.

The code below imports both the default export (ascheesburger) as well as all named exports.

import{defaultascheeseburger,bun,cheese,patty,}from"./cheeseburger.mjs";
Enter fullscreen modeExit fullscreen mode

We can tidy it up a smidge by splitting the default export and named export import statements — using a comma.

- import {- default as cheeseburger,- bun,- cheese,- patty,- } from "./cheeseburger.mjs";+ import cheeseburger, { bun, cheese, patty } from "./cheeseburger.mjs";
Enter fullscreen modeExit fullscreen mode

This eliminates the need to rename thedefault on import withas.

What mixed imports are not#

The import position of default and named exports cannot be swapped. When mixing the two, it's always default first then named exports.

When I first saw this syntax, I assumed that every comma was like a repeaet — a new opportunity to assign local variables. That's not how it is. One comma, after the default, and before the named.

Go pro#

This is part of a course I'm building on modules atlunch.dev.

When live, members get access to this and other courses on React.

Join lunch.dev for videos

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

software explorer
  • Work
    Host and Producer at React Podcast
  • Joined

More fromchantastic

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp