Named Exports vs Default Exports
What you'll learn: The difference between named and default exports, and why many React developers prefer named exports.
Two Ways to Export Components
When you create a React component in its own file, you need to export it so other files can import and use it. JavaScript gives you two options:
Default Export (one per file):
// Button.js
function Button() {
return <button>Click me</button>;
}
export default Button;
// App.js
import Button from './Button'; // Can use any name here
import MyBtn from './Button'; // This also works!
Named Export (multiple per file):
// Button.js
export function Button() {
return <button>Click me</button>;
}
// App.js
import { Button } from './Button'; // Must match exact name
The Tradeoffs
Default exports offer flexibility—you can rename on import. But this flexibility is a double-edged sword: inconsistent naming across files makes codebases harder to navigate.
Named exports enforce consistency. The component name in your import must match the export. This prevents confusion when multiple developers work on the same codebase. You can't accidentally call Button something different in ten different files.
Named exports also support multiple exports from one file, though for components, one-component-per-file is still best practice.
Why Named Exports Win
Most React teams prefer named exports because:
- Refactoring tools work better (rename refactoring updates all imports automatically)
- Code search is reliable (searching for "Button" finds all uses)
- No naming debates ("Do we call this
PrimaryButtonorMainButtonwhen importing?")
Key Takeaway: While both export styles work in React, named exports provide better consistency, refactoring support, and searchability—making them the preferred choice for component exports in team codebases.