Components
Build your own reusable UI pieces — and compose them like LEGO bricks.
What you will learn
- Create function components
- Use one component inside another
- Follow the naming rules
A component is a function that returns JSX
Define a component as a function whose name starts with a capital letter. It returns JSX. Then use it like an HTML tag: <MyComponent />.
function Welcome() {
return <h2>Welcome to CodingClave!</h2>;
}
function App() {
return (
<div>
<Welcome />
<p>The heading above comes from the Welcome component.</p>
</div>
);
}Two functions here. Welcome is a small component that returns one heading. App is the main component, and inside its markup it uses <Welcome /> — exactly like an HTML tag. When React draws App, it sees <Welcome /> and swaps in whatever Welcome returns. This is composition: building a bigger UI by placing smaller components inside it.
Note: Output:
Welcome to CodingClave!
The heading above comes from the Welcome component.
The first line came from the Welcome component; the second from App itself.
Compose and reuse
function Card() {
return (
<div style={{ border: "1px solid #ccc", borderRadius: 10, padding: 12, margin: 6 }}>
<h4>A reusable card</h4>
<p>Used as many times as you like.</p>
</div>
);
}
function App() {
return (
<div>
<Card />
<Card />
<Card />
</div>
);
}We wrote the Card component once (a bordered box with a heading and text), then used <Card /> three times inside App. React renders three identical cards. Change the Card definition in one place and all three update together — that is the power of reusable components. (The style={{ ... }} is an inline style object; styling is covered in its own lesson.)
Note: Output: Three identical bordered cards stacked down the page, each showing A reusable card and “Used as many times as you like.”
Watch out: Component names must start with a capital letter (Card, not card). React treats lowercase names as ordinary HTML tags.
Tip: Each component usually lives in its own file (Card.jsx) and is exported, then imported where needed. This keeps big apps organised.
Q. What must a React component name start with?
✍️ Practice
- Create a
Headercomponent and use it inApp. - Create a
Cardcomponent and render it three times.
🏠 Homework
- Build a small page from three components:
Header,ContentandFooter.