DEV Community

Cover image for Import/Export: Default vs Named (And Why Some Use Braces)
Mahmudur Rahman
Mahmudur Rahman

Posted on

Import/Export: Default vs Named (And Why Some Use Braces)

If you've worked with React or JavaScript modules, you might have noticed that sometimes you write imports with curly braces ({}) and sometimes without them. Ever wondered why?

Let’s break it down.


πŸ“¦ Default Export

A default export allows a module to export a single value. It’s useful when your file exports just one main thing β€” like a component.

πŸ‘‰ Example:

// components/Footer.jsx
export default function Footer() {
  return <footer>...</footer>;
}

Enter fullscreen mode Exit fullscreen mode

βœ… Importing it:

import Footer from './components/Footer';

Enter fullscreen mode Exit fullscreen mode

🚫 No curly braces needed for default exports.


πŸ”§ Named Export

A named export allows you to export multiple values from a file. You must import them using curly braces.

πŸ‘‰ Example:

// components/Contact.tsx
export function Contact() { ... }
export function AnotherHelper() { ... }

Enter fullscreen mode Exit fullscreen mode

βœ… Importing them:

import { Contact } from './components/Contact';

Enter fullscreen mode Exit fullscreen mode

βœ… Curly braces required for named exports.


Quick Summary


πŸ’‘ Tip

You can combine both in one file:

export default function App() { ... }
export function Helper() { ... }

Enter fullscreen mode Exit fullscreen mode

Then import like:

import App, { Helper } from './App';

Enter fullscreen mode Exit fullscreen mode

Hope this clears up the mystery behind those curly braces!

Do you have questions or tips? Drop them in the comments.


Follow for more
@mahmud-r-farhan https://devplus.fun

Top comments (0)

OSZAR »