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.
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:
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:
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.
- In
App.jsx, changeimport { Footer }toimport Footer(no braces). - In
App.jsx, change"./components/Header.jsx"to"./components/header.jsx"(lower-case h). - In
TodayCard.jsx, delete the worddefault.
Check your predictions
Footer.jsxhas no default export, so the import gets nothing. React reports that the element type is invalid and gotundefined. When you see "got: undefined" in React, check that the import matches the export.- 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. App.jsximports a default that no longer exists. You get the same "got: undefined" error as in the first change, this time forTodayCard.
Your turn
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.
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:
export default function TipCard() {
return (
<section className="card">
<h2>Tip</h2>
<p>Press Ctrl + Enter to run.</p>
</section>
);
}And in App.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.
Completion is saved in this browser only. It does not measure mastery.