Skip to content

Components in Files

The Course
•
20 min study

One file with every component works for three components. It doesn't work for thirty. React projects usually give each component its own file and put those files in a src/components folder. JavaScript modules connect the files: one file exports a component, and another imports it.

The same page, in separate files

This is the page from the previous lesson, split up. Open each file under src/components, then come back to App.jsx.

Components in their own files React · 8 files
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Components in files</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html> import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; const root = createRoot(document.getElementById("root")); root.render(<App />); import Header from "./components/Header.jsx"; import TodayCard from "./components/TodayCard.jsx"; import { Footer } from "./components/Footer.jsx"; export default function App() { return ( <main> <Header /> <TodayCard /> <TodayCard /> <Footer /> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } .header h1 { margin: 0; } .header p { margin: 0 0 1rem; color: #087ea4; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } export function Footer() { return <footer>Built with React</footer>; } export default function Header() { return ( <header className="header"> <h1>Study Tasks</h1> <p>Seven days of React</p> </header> ); } // Replace null with the tip card JSX, then import this component in App.jsx. export default function TipCard() { return null; } export default function TodayCard() { return ( <section className="card"> <h2>Today</h2> <p>Split components into files.</p> </section> ); }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Each component file ends up as a small, self-contained unit, and App.jsx now reads like a table of contents for the page.

Default and named exports

There are two ways to export, and this lab uses both.

Default export. Header.jsx says export default function Header(). A file can have one default export. The importing file chooses the name, with no braces:

JavaScript
import Header from "./components/Header.jsx";

Named export. Footer.jsx says export function Footer(), with no default. A file can have many named exports. The importing file must use the exact name, inside braces:

JavaScript
import { Footer } from "./components/Footer.jsx";

Most React projects use one default export per component file. Named exports are handy when a file holds a few small, related pieces. Whichever you choose, the import has to match the export.

Paths

"./components/Header.jsx" is a path relative to the file doing the importing. ./ means "the folder this file is in". App.jsx is in src/, so this path points to src/components/Header.jsx. Inside src/components/Header.jsx, a path back to App.jsx would start with ../, meaning "one folder up".

Break the connection on purpose

Try each change below. Predict the result first, run, read the console, then undo the change before trying the next one.

  1. In App.jsx, change import { Footer } to import Footer (no braces).
  2. In App.jsx, change "./components/Header.jsx" to "./components/header.jsx" (lower-case h).
  3. In TodayCard.jsx, delete the word default.
Check your predictions
  1. Footer.jsx has no default export, so the import gets nothing. React reports that the element type is invalid and got undefined. When you see "got: undefined" in React, check that the import matches the export.
  2. The lab reports it can't find the file ./components/header.jsx. File names are case-sensitive. Your own computer may forgive this, but most servers won't, so match the case exactly.
  3. App.jsx imports a default that no longer exists. You get the same "got: undefined" error as in the first change, this time for TodayCard.

Your turn

Exercise

Move a component out

In the lab above, App shows two TodayCards. Replace the second one with a TipCard component (heading Tip, paragraph Press Ctrl + Enter to run.). This time, put TipCard in its own file, src/components/TipCard.jsx, with a default export.

src/components/TipCard.jsx is already in the folder tree. It returns null until you fill it in. Replace that placeholder, then import it into App.jsx. You don't need to install anything to do this exercise.

Your working space React · 8 files
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Components in files</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html> import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; const root = createRoot(document.getElementById("root")); root.render(<App />); import Header from "./components/Header.jsx"; import TodayCard from "./components/TodayCard.jsx"; import { Footer } from "./components/Footer.jsx"; export default function App() { return ( <main> <Header /> <TodayCard /> <TodayCard /> <Footer /> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } .header h1 { margin: 0; } .header p { margin: 0 0 1rem; color: #087ea4; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } export function Footer() { return <footer>Built with React</footer>; } export default function Header() { return ( <header className="header"> <h1>Study Tasks</h1> <p>Seven days of React</p> </header> ); } // Replace null with the tip card JSX, then import this component in App.jsx. export default function TipCard() { return null; } export default function TodayCard() { return ( <section className="card"> <h2>Today</h2> <p>Split components into files.</p> </section> ); }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

One file exports TipCard, and App.jsx imports it. Write the export first, then the import, then use <TipCard />.

Hint 2: a first step

Open TipCard.jsx and replace null with a single card element. Then look at how App.jsx imports TodayCard: a default export uses the same import shape.

Show one solution

In its own file, src/components/TipCard.jsx:

JSX
export default function TipCard() {
  return (
    <section className="card">
      <h2>Tip</h2>
      <p>Press Ctrl + Enter to run.</p>
    </section>
  );
}

And in App.jsx:

JSX
import Header from "./components/Header.jsx";
import TodayCard from "./components/TodayCard.jsx";
import TipCard from "./components/TipCard.jsx";
import { Footer } from "./components/Footer.jsx";

export default function App() {
  return (
    <main>
      <Header />
      <TodayCard />
      <TipCard />
      <Footer />
    </main>
  );
}

If you kept it in TodayCard.jsx as export function TipCard(), the import is import TodayCard, { TipCard } from "./components/TodayCard.jsx";. That one line takes the default export and a named export together.

Was this article helpful?

Your response is saved on this device.