← Back to Chapters

Default Exports vs Named Exports

? Default Exports vs Named Exports

⚛️ Introduction

In JavaScript modules (and therefore React), components and functions can be exported in two ways — Default Exports and Named Exports. Understanding their difference is crucial for writing maintainable and scalable React code.

? Default Exports

A module can have only one default export. It’s imported without curly braces, and the importing name can be anything. Default exports are ideal for modules exporting a single main component or function.

? View Code Example
// Button.js - Exporting a component as default
export default function Button() {
  return <button>Click Me</button>;
}

// Importing in another file
import Button from "./Button";   // ✅ Works
import MyBtn from "./Button";    // ✅ Works (name can be changed)

? Named Exports

Named exports allow multiple items to be exported from a single file. They must be imported using the same name within curly braces. Named exports are best when a module contains multiple related utilities or components.

? View Code Example
// utils.js - Exporting multiple functions
export function add(a, b) { return a + b; }
export function subtract(a, b) { return a - b; }

// Importing specific functions
import { add, subtract } from "./utils";  // ✅ Must match names

⚖️ Comparison Table

Aspect Default Export Named Export
Quantity per file Only one allowed Multiple allowed
Import syntax No curly braces Requires curly braces
Import name flexibility Can rename freely Must match exported name
Use case Single component per file Utility or constants module
Example export default App export const Button

? Mixed Example

Combining default and named exports through an index.js file helps organize large projects and simplifies imports.

? View Code Example
// components/index.js - Aggregating exports
export { default as Button } from "./Button";
export { default as Header } from "./Header";
export const VERSION = "1.0";

// Import elsewhere using named imports
import { Button, Header, VERSION } from "./components";

? Interactive Playground

Switch between modes to see how the syntax changes for both the exporting and importing files.

Profile.js
export default function Profile() {
  ... 
}
➡️
App.js
import AnyName from "./Profile";
Note: With Default Exports, you can name the import whatever you want (e.g., "AnyName").

? Tips

  • Use default exports for major components (e.g., App, Navbar).
  • Use named exports for utility functions and constants.
  • Avoid mixing export types in the same file unless you’re building a module index.
  • Maintain consistent export style across your project for clarity.

? Try This

  1. Create a MathUtils.js file and export multiple functions using named exports.
  2. Create a Button.js file with a default export component.
  3. Use both in App.js to practice mixed imports.
  4. Experiment by renaming imports and observe behavior.

Goal: Learn when and how to use default vs named exports for better modular design in React.