Your first component
A component is the building block of every React interface: a button, a product card, a whole page. In this lesson you will write your first component, use it inside another one and learn the three rules React depends on.
What a component is
Look at any website: a header, a menu, cards, buttons, a footer. Many parts repeat. Ten product cards differ only in the picture, the name and the price. In plain HTML you copy that card ten times and then edit all ten copies when the designer changes a margin.
React takes a different approach. You describe the card once as a component and reuse it as many times as you need. Change the component and every card changes with it. Small components build bigger ones: buttons make a form, a form and cards make a page, pages make an app.
Technically, a React component is a plain JavaScript function that returns markup. That is all. No classes, no configuration, no special files.
A component in three lines
Here is the simplest possible component. It is called Greeting and shows a heading:
function Greeting() {
return <h1>Hello, React!</h1>;
}
It looks like a function that returns a piece of HTML right in the middle of JavaScript. This syntax is called JSX, and it lets you write markup inside code. We will cover JSX in detail in a later lesson. For now it is enough to know that <h1>…</h1> inside the function is not a string and not a mistake: it describes what should appear on the screen.
Below is a live example: code on the left, result on the right. Sign in with GitHub or Google and the code becomes editable: change the heading text and the preview updates on its own. The Run button or Ctrl/⌘ + Enter re-runs the code manually.
function Greeting() {
return <h1>Hello, React!</h1>;
}
export default function App() {
return <Greeting />;
}
Look at the last line: <Greeting />. That is how you use a component: like an HTML tag with its name. export default tells the sandbox which component is the main one, and that is the one it renders.
Three rules of a component
1. It is a function that returns markup. If the function returns nothing, React shows nothing. You return one root element, for example a <div> with anything inside. When the markup spans several lines, wrap it in parentheses:
function Profile() {
return (
<div>
<h2>Anna</h2>
<p>Learns React in the evenings</p>
</div>
);
}
2. The name starts with a capital letter. Profile, not profile. React uses the first letter to tell your components from regular HTML tags: <div> and <section> are browser tags, <Profile /> is your component. Lowercase the name and React treats it as an unknown HTML tag and renders nothing useful.
3. Keep the parenthesis on the same line as return. If you write return and move the markup to the next line without parentheses, JavaScript inserts a semicolon right after return and the function returns undefined. Parentheses protect you from that trap.
Components inside components
The power of components is that they nest. The App component below uses Profile three times. The card is written once, and there are three cards on the screen:
function Profile() {
return (
<div className="card">
<h2>Anna</h2>
<p>Learns React in the evenings</p>
</div>
);
}
export default function App() {
return (
<section>
<h1>Our team</h1>
<Profile />
<Profile />
<Profile />
</section>
);
}
All three cards are identical for now. To show three different people you pass data to the component through props, which get a lesson of their own. What matters now is that the page is assembled from components like a construction set.
One more detail: JSX uses className, not class. The word class is reserved in JavaScript, so React uses the DOM property name.
How React puts a component on the page
In the sandbox the main component is shown automatically. In a real project two lines do this, usually written once in main.jsx:
import { createRoot } from 'react-dom/client';
import App from './App.jsx';
createRoot(document.getElementById('root')).render(<App />);
React finds the empty <div id="root"> on the HTML page, calls App, gets the markup and turns it into real page elements. We will set up such a project on your computer in the tooling module. Until then all practice happens right in the browser.
Summary
- A component is a JavaScript function that returns markup.
- A component name starts with a capital letter, otherwise React treats it as an HTML tag.
- Wrap multi-line markup after
returnin parentheses. - Components nest inside each other but are declared at the top level of the file.
- The main component is exported with
export default.
Build a page of three cards
Write a Card component with an <h2> heading and a short text, and use it in App three times. Put a shared <h1> heading above the cards. Hint: the card’s root element needs the class card, the checks look for it. When you are done, press Check.
// 1. Declare a Card component: div.card with an h2 and a p inside
// 2. Use <Card /> in App three times under the h1 heading
export default function App() {
return (
<main>
<h1>My favourite books</h1>
</main>
);
}
Sign in with GitHub or Google to solve and check the homework. It is free; your code and progress are saved.
- The page has an h1 heading with text
- There are at least three elements with the class card
- Every card has an h2 heading
Your solution is saved to your account; the lesson is marked done once the checks pass.
Still have a question about this lesson?
Tell us what is unclear. The question goes to the author, and the answer to your email if you leave one. Later an AI tutor will answer here.