So far App has done all the work. Real pages are built from many smaller
pieces: a header, a card, a list, a button. In React, each piece is a
component: a JavaScript function that returns JSX.
Two components inside App
Read the lab before running it. App uses two components you haven't seen
before, Header and TodayCard. Both are defined in the same file, above
App.
npm install and npm run dev. This lab has four files, so they're listed as a folder tree beside the code instead of as tabs. Labs with more than three files work this way from now on. Click a folder to open or close it, and drag the divider to widen the tree.
Look at the JSX inside App. <Header /> doesn't mean "an HTML header
element". It means "call the Header function here and put whatever it returns
in this spot". The page you see is the result of React calling App, which
uses Header and TodayCard, and fitting their JSX together:
function Header() {
return (
<header className="header">
<h1>Study Tasks</h1>
<p>Seven days of React</p>
</header>
);
}
export default function App() {
return (
<main>
<Header />
<TodayCard />
</main>
);
}Where the styles come from
main.jsx imports styles.css, and React's build tool adds it to the page.
className="card" in the JSX matches the .card rule in that file. Styling
isn't the focus today, but you can change a colour in styles.css and run the
lab to see the effect.
Component names start with a capital letter
React tells components and HTML elements apart by the first letter.
<header> in lower case is the HTML element. <Header> with a capital H is
your component.
Try it. Rename the function TodayCard to todayCard, and change
<TodayCard /> inside App to <todayCard />. Predict what happens, then
run.
Check your prediction
There's no compile error, but the card disappears. React treats <todayCard />
as an unknown HTML element, and the console shows a warning that this tag isn't
recognised in this browser. If it was meant to be a React component, the name
should start with an uppercase letter. Change both names back.
Reuse
You can render the same component more than once with JSX. Let React call the
function; write <TodayCard />, rather than calling TodayCard() yourself. Add a second
<TodayCard /> under the first and run the lab. You get two identical cards.
Two identical cards aren't very useful yet. On Day 2 you'll pass each card different data with props, so one component can show "Today" in one place and "Tomorrow" in another.
Your turn
Add a tip card
Add a third component called TipCard. It shows a heading Tip and the
paragraph Press Ctrl + Enter to run. Show it under TodayCard. Give its
section the same className so it looks like a card.
npm install and npm run dev. Hint 1: the idea
TipCard looks almost exactly like TodayCard. Define it above App, and
start its name with a capital letter.
Hint 2: a first step
Defining a function doesn't show anything. You also have to use it as
<TipCard /> inside the JSX that App returns.
Show one solution
function TipCard() {
return (
<section className="card">
<h2>Tip</h2>
<p>Press Ctrl + Enter to run.</p>
</section>
);
}
export default function App() {
return (
<main>
<Header />
<TodayCard />
<TipCard />
</main>
);
}Header and TodayCard stay as they were.
Completion is saved in this browser only. It does not measure mastery.