Skip to content

Components

The Course
•
20 min study

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.

Header and card React · 4 files
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Components</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 />); function Header() { return ( <header className="header"> <h1>Study Tasks</h1> <p>Seven days of React</p> </header> ); } function TodayCard() { return ( <section className="card"> <h2>Today</h2> <p>Learn what a component is.</p> </section> ); } export default function App() { return ( <main> <Header /> <TodayCard /> </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; }
Runs with Vite on your computer: unzip, then 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:

JSX
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

Exercise

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.

Your working space React · 4 files
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Components</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 />); function Header() { return ( <header className="header"> <h1>Study Tasks</h1> <p>Seven days of React</p> </header> ); } function TodayCard() { return ( <section className="card"> <h2>Today</h2> <p>Learn what a component is.</p> </section> ); } export default function App() { return ( <main> <Header /> <TodayCard /> </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; }
Runs with Vite on your computer: unzip, then 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
JSX
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.

Was this article helpful?

Your response is saved on this device.