In React, you usually split your UI into multiple files — typically one component per file. To make a component available in other files, you export it from its file and import it where you want to use it.
React (via ES Modules) supports two main export styles:
Understanding when to use each style keeps your React project organized, readable, and easy to maintain.
A default export lets you mark one main value as the “default” thing a file provides. This is commonly used for major components like App, Layout, or feature-specific components.
// Header.js - default export component
export default function Header() {
return <h2>Welcome to My Website</h2>;
}
Basic import:
// App.js - importing the default Header component
import Header from "./Header";
function App() {
return (
<div>
<Header />
</div>
);
}
The name you use when importing a default export is not required to match the original function or component name.
// App.js - renaming the default import
import MyHeader from "./Header"; // Works fine
function App() {
return (
<div>
<MyHeader />
</div>
);
}
A named export lets you export multiple things from the same file — for example several small components, helper functions, or constants.
When importing, you must use the exact exported name, wrapped in curly braces { }.
// components.js - two components as named exports
export function Navbar() {
return <h2>Navigation Bar</h2>;
}
export function Footer() {
return <p>© 2025 React Learning Portal</p>;
}
// App.js - importing named exports with curly braces
import { Navbar, Footer } from "./components";
function App() {
return (
<div>
<Navbar />
<Footer />
</div>
);
}
Here, both Navbar and Footer are imported by their exact names. If you change the name Navbar to something else in the import, it will no longer work.
You can mix one default export with multiple named exports in the same file. This is useful when a file has one main component plus some related helpers or smaller components.
// Layout.js - default + named export in the same file
export default function Layout() {
return <div>Main Layout</div>;
}
export function Sidebar() {
return <aside>Sidebar</aside>;
}
// App.js - importing default and named export together
import Layout, { Sidebar } from "./Layout";
function App() {
return (
<div>
<Layout />
<Sidebar />
</div>
);
}
? Cheat Sheet · Default vs Named
| Feature | Default Export | Named Export |
|---|---|---|
| Syntax (export) | export default Component |
export const Component = ... |
| Syntax (import) | import Comp from "./file" |
import { Comp } from "./file" |
| Rename on import | ✅ Allowed (e.g. import X from "./file") |
❌ Must match exactly (e.g. Comp must stay Comp) |
| Number of exports | Only one default export per file | Multiple named exports per file |
With the default export example:
Header.js exports a default component that returns <h2>Welcome to My Website</h2>.App.js imports that component and renders <Header /> inside a <div>.Rendered output in the browser:
A level-2 heading on the page that displays: Welcome to My Website
If you rename the import to MyHeader, the visual output is still the same — React doesn’t care about the import variable name for default exports, only that the file exported something by default.
App or Layout.Header.js contains Header).Header.js (default export) and utils.js (named exports).utils.js, export two functions using named exports: getDate() and getYear().App.js, import the default Header component and the named utilities from utils.js, then display their values on the screen.import MyHeader from "./Header") and confirm that it still works.Goal: Clearly understand the difference between default and named exports, how to import them, and how to organize reusable components and utilities across multiple files in a React project.
// Example structure showing default + named imports
// Header.js
export default function Header() {
return <h1>My React App</h1>;
}
// utils.js
export function getDate() {
return new Date().toLocaleDateString();
}
export function getYear() {
return new Date().getFullYear();
}
// App.js
import MyHeader from "./Header";
import { getDate, getYear } from "./utils";
function App() {
return (
<div>
<MyHeader />
<p>Today is: {getDate()}</p>
<p>Current year: {getYear()}</p>
</div>
);
}