Components & Props Lesson 4 of 13
React Components
Build user interfaces from function components — create, reuse and nest components, split a page into Header, Main and Footer, and follow naming rules.
A component is a JavaScript function that returns JSX. Components are like LEGO bricks: you build small pieces (a button, a card, a header) and combine them into whole pages. Each one can be reused as many times as you want.
Creating and using a component
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
function Welcome() {
return <p>👋 Welcome to Aral Code!</p>;
}
function App() {
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<h2>My Page</h2>
<Welcome />
<Welcome />
<Welcome />
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
- A component is a function whose name starts with a capital letter.
- It returns JSX.
- You use it like an HTML tag:
<Welcome />.
Splitting a page into components
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<style>
body { margin: 0; font-family: sans-serif; }
header, footer { background: #0f5c4d; color: white; padding: 12px 16px; }
main { padding: 16px; }
.card { border: 1px solid #ddd; border-radius: 10px; padding: 12px; margin: 8px 0; }
</style>
<script type="text/babel">
function Header() {
return <header><strong>🏫 Rizal NHS</strong></header>;
}
function Announcement() {
return (
<div className="card">
<h3>Foundation Day</h3>
<p>Classes are suspended on Friday for the school's 50th anniversary.</p>
</div>
);
}
function Footer() {
return <footer>© 2026 Rizal National High School</footer>;
}
function App() {
return (
<>
<Header />
<main>
<h2>Announcements</h2>
<Announcement />
</main>
<Footer />
</>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
App is the root component; it contains the others. This structure is called the component tree:
App
├── Header
├── main
│ └── Announcement
└── Footer
Why components?
- Reuse: write a ProductCard once, use it for 100 products.
- Organization: each file or function handles one part of the page.
- Teamwork: different people can build different components.
- Easier fixes: a bug in the navbar lives in the Navbar component.
Arrow function components
Components can also be written as arrow functions. Both styles are common:
const Badge = () => <span className="badge">NEW</span>;
Components in separate files
In a Vite project, each component usually has its own file:
// src/components/Header.jsx
export default function Header() {
return <header>🏫 Rizal NHS</header>;
}
// src/App.jsx
import Header from "./components/Header.jsx";
export default function App() {
return <Header />;
}
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04