Web Development courseLesson 12 of 20 · tap to see all lessons
Web Development · Beginner
React Fundamentals: Components, JSX, Composing Reusable Components, and Props
Learn component-based development with React: write JSX, build small reusable components, compose them into pages, and pass data with props, all running in your browser.
- Qyubie Learn Editorial Team
- Published
- 16 min read
Includes 2 exercises and 2 quick checks you can do right here.
In lesson 7 you changed pages by hand: find an element, change its text, create another, append it. That works for a counter, but in a real app with hundreds of parts, keeping the page in step with the data becomes a mess. React is a JavaScript library that fixes this. You describe what the page should look like for the current data, and React updates the real page for you. Next.js, in lesson 14, is built on React.
By the end of this lesson you will be able to:
- explain component-based development and why teams use it,
- write JSX and follow its rules,
- create components and compose them into bigger ones, and
- pass data into components with props, including
children.
Component-based development
Look at any app screen and you'll see it's made of pieces: a navigation bar, a search box, a list of cards, a footer. In React, each piece is a component, a small, self-contained part of the interface with its own markup, style, and behavior.
App
├── Header
├── StudentList
│ ├── StudentCard (Ana)
│ ├── StudentCard (Ben)
│ └── StudentCard (Cara)
└── FooterBuilding with components gives you:
- Reuse. Write
StudentCardonce and use it a hundred times, with different data each time. - Focus. Each component is small enough to understand and test on its own.
- Teamwork. One person builds
Headerwhile another buildsStudentList. - Consistency. Every card looks and behaves the same, because it's the same code.
A React component is just a JavaScript function that returns markup:
function Greeting() {
return <h1>Hello, BSCS 3A!</h1>;
}That <h1> inside JavaScript is JSX, explained below.
Running React on this page
A React project normally needs a build tool (you'll create one at the end of this lesson). So you can try React right now, each example on this page loads React from a CDN and translates JSX in the browser with Babel. The setup lines at the top of each example are always the same:
- the import map says where
reactandreact-domcome from, - the Babel script translates JSX into plain JavaScript, and
- your code goes in the
<script type="text/babel" data-type="module">tag.
Your code starts with import React from "react" because this browser setup needs it; in a Vite or Next.js project you can leave that line out. The previews need an internet connection.
Here's the smallest React app. createRoot connects React to the empty <div id="root">, and render shows a component there:
Try it · HTML & CSS
JSX and component structure
JSX lets you write HTML-like markup inside JavaScript. It isn't HTML, though: a build tool turns each tag into a JavaScript function call, so JSX follows a few stricter rules.
| Rule | HTML | JSX |
|---|---|---|
| Return one parent element | two siblings are fine | wrap them in a <div> or a fragment <>...</> |
| Close every tag | <br>, <img src="a.png"> | <br />, <img src="a.png" /> |
class is a JavaScript keyword | class="card" | className="card" |
for is a JavaScript keyword | <label for="email"> | <label htmlFor="email"> |
| Attributes are camelCase | onclick, tabindex | onClick, tabIndex |
| Inline styles are objects | style="color: red" | style={{ color: "red" }} |
Curly braces: JavaScript inside JSX
Inside JSX, { } opens a window back into JavaScript. Put any expression there: a variable, a calculation, a function call, a ternary.
function StudentSummary() {
const name = "Ana Reyes";
const grades = [92, 88, 95];
const average = grades.reduce((sum, g) => sum + g, 0) / grades.length;
return (
<>
<h2>{name}</h2>
<p>Average: {average.toFixed(1)}</p>
<p>{average >= 90 ? "With honors" : "Regular"}</p>
<img src="/qyubie-mark.png" alt={`Photo of ${name}`} width={48} />
</>
);
}Two things to notice:
- Wrap multi-line JSX in parentheses after
return, or JavaScript returns nothing. - Statements like
ifandforcan't go inside{ }; only expressions can. Do the work above thereturn, or use a ternary (? :).
Try it, then change grades and watch the output change:
Try it · HTML & CSS
Quick check
Creating and composing reusable components
Component names must start with a capital letter. That's how React tells <StudentCard /> (your component) from <div> (an HTML tag). Use a component like a tag, as many times as you want.
Composing means building bigger components out of smaller ones. An App holds a Header and a list; the list holds cards:
Try it · HTML & CSS
Both cards say the same thing, which isn't very useful yet. Props fix that, next.
One component per file
In a real project, each component lives in its own file and is shared with export/import from lesson 11:
// src/components/StudentCard.jsx
export default function StudentCard() {
return (
<div className="card">
<h3>Student name</h3>
</div>
);
}// src/App.jsx
import Header from "./components/Header.jsx";
import StudentCard from "./components/StudentCard.jsx";
export default function App() {
return (
<main>
<Header />
<StudentCard />
</main>
);
}Two good habits: name the file after the component, and keep components small. When a component grows past what fits on a screen, split part of it into a new component.
Props: passing data to components
Props (short for properties) are how a parent component passes data to a child, the same way HTML attributes configure a tag. You pass them like attributes:
<StudentCard name="Ana Reyes" course="BSCS" year={3} />Text can use quotes; anything else (numbers, booleans, arrays, objects, functions) goes in { }. React collects them into one object and passes it as the function's first argument. Most people destructure it right in the parameter list:
function StudentCard({ name, course, year }) {
return (
<div className="card">
<h3>{name}</h3>
<p>{course} {year}</p>
</div>
);
}Now one component shows any student:
Try it · HTML & CSS
What's happening:
honors = falseis a default value, used when the parent leaves the prop out (Ben and Cara).{honors && <Badge ... />}shows the badge only whenhonorsis true. Lesson 13 covers this kind of conditional rendering.Badgeis itself a component with a prop, used insideStudentCard. Data flows down:App→StudentCard→Badge.
Props are read-only
A component must never change its own props. name = "Someone else" inside StudentCard is a bug: the parent owns that data. When a component needs data that changes (a counter, a typed value), it uses state, the topic of lesson 13.
The children prop
Whatever you put between a component's opening and closing tags arrives as a special prop called children. It lets you build wrapper components, like a card frame that can hold anything:
function Panel({ title, children }) {
return (
<section className="panel">
<h2>{title}</h2>
{children}
</section>
);
}
function App() {
return (
<Panel title="Announcements">
<p>No classes on Friday.</p>
<p>Project proposals are due Monday.</p>
</Panel>
);
}Panel doesn't know or care what's inside; it just draws the frame. Layouts in Next.js work exactly this way.
Quick check
Exercise · HTML & CSS
Subject card component
Create a SubjectCard component that takes code, title, and units props and shows them (for example a heading with the code and title, and 3 units). Use it in App for two subjects: CS 324 Web Development (3 units) and CS 122 Object-Oriented Programming (3 units).
Hint
function SubjectCard({ code, title, units }) { return <div><h3>{code}: {title}</h3><p>{units} units</p></div>; } then <SubjectCard code="CS 324" title="Web Development" units={3} /> in App.
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
<div id="root"></div>
<script type="importmap">
{ "imports": {
"react": "https://esm.sh/react@19.3.1",
"react-dom/client": "https://esm.sh/react-dom@19.3.1/client?external=react"
} }
</script>
<script src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script>
<script type="text/babel" data-type="module">
import React from "react";
import { createRoot } from "react-dom/client";
function SubjectCard({ code, title, units }) {
return (
<div className="card">
<h3>{code}: {title}</h3>
<p>{units} units</p>
</div>
);
}
function App() {
return (
<main>
<h1>My Subjects</h1>
<SubjectCard code="CS 324" title="Web Development" units={3} />
<SubjectCard code="CS 122" title="Object-Oriented Programming" units={3} />
</main>
);
}
createRoot(document.getElementById("root")).render(<App />);
</script>
Exercise · HTML & CSS
A reusable Notice frame
Write a Notice component that draws a box with a bold title prop and shows its children below the title. Use it once with the title Reminder and a paragraph inside: Bring your laptop on Friday.
Hint
function Notice({ title, children }) { return <div className="notice"><strong>{title}</strong>{children}</div>; }
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
<div id="root"></div>
<script type="importmap">
{ "imports": {
"react": "https://esm.sh/react@19.3.1",
"react-dom/client": "https://esm.sh/react-dom@19.3.1/client?external=react"
} }
</script>
<script src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script>
<style>
.notice { border-left: 4px solid #7c3aed; background: #f5f3ff; padding: 10px 14px; }
</style>
<script type="text/babel" data-type="module">
import React from "react";
import { createRoot } from "react-dom/client";
function Notice({ title, children }) {
return (
<div className="notice">
<strong>{title}</strong>
{children}
</div>
);
}
function App() {
return (
<main>
<Notice title="Reminder">
<p>Bring your laptop on Friday.</p>
</Notice>
</main>
);
}
createRoot(document.getElementById("root")).render(<App />);
</script>
Creating a React project on your computer
When you're ready to work outside this page, the quickest standalone setup uses Vite, a fast build tool. With Node.js installed (lesson 11; Vite needs version 20.19 or newer), run the commands below, and answer any questions it asks with the default:
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run devOpen the address it prints (usually http://localhost:5173). Edit src/App.jsx, save, and the browser updates instantly. Lesson 14 uses Next.js instead, which adds routing, layouts, and a server on top of React.
Common mistakes
- Lowercase component names (
<studentCard />): React looks for an HTML tag and renders nothing useful. - Returning two elements without a parent. Wrap them in
<>...</>. classinstead ofclassName, and unclosed tags like<img>or<br>.- Quoting numbers in props (
year="3"passes the text"3"); useyear={3}. - Changing props inside the component. Props belong to the parent.
- Forgetting parentheses around multi-line JSX after
return.
Summary
- React builds interfaces from components: functions, named with a capital letter, that return JSX.
- JSX looks like HTML but follows JavaScript rules: one parent, closed tags,
className, camelCase attributes, and{ }for expressions. - Components compose: big components are made of small ones, and each usually lives in its own file.
- Props pass data from parent to child, are read-only, can have default values, and
childrenholds whatever is placed between the tags.
Next: state, which lets components remember and change data when users click and type.