Modern Full-Stack & React Internals · Chapter 1 · ~15 min · Beginner
Your First React Component & JSX
Learn how React transforms frontend development by composing user interfaces out of reusable, modular components using JSX (HTML inside JavaScript).
- Understand why React uses Components instead of huge HTML files
- Master the 3 Golden Rules of JSX syntax
- Embed dynamic JavaScript data inside markup with {curly braces}
- Pass properties (props) to create customizable, reusable UI blocks
1UI Made of Lego Bricks: The Component Mindset
Before React, web pages were built as huge, monolithic HTML files. If your website had a "User Profile Card" on 10 different pages, you had to copy and paste the exact same 30 lines of HTML 10 times. When you needed to change the avatar size, you had to update 10 files and pray you didn't miss one!
React solves this by introducing Components. A component is a self-contained, reusable piece of UI — like a button, a navigation bar, or a card. Think of components like Lego bricks: you design each brick once, and then snap them together to build complex websites.
Write Once, Use Anywhere
Define a <Button> once and reuse it across 50 pages with different labels and colors.
Pure JavaScript Functions
A React component is simply a JavaScript function that returns what should appear on the screen!
Isolated Scope
Changes made inside one component don't accidentally break unrelated styling elsewhere on the page.
What is the primary benefit of dividing a website into React components?
2Writing Your First Component
In React, a component is just a JavaScript function whose name starts with a Capital Letter, and that returns HTML-like markup called JSX:
function WelcomeCard() {
return (
<div className="card">
<h2>Welcome to Coders Hub!</h2>
<p>Start learning React step by step.</p>
</div>
);
}Why must custom React components begin with a capital letter (e.g., 'function Header()' instead of 'function header()')?
3The 3 Golden Rules of JSX
JSX looks almost identical to regular HTML, but because it runs inside JavaScript, it follows 3 strict rules:
Rule 1: Return a Single Root Element
A function can only return one value. If you have multiple sibling elements, wrap them in a single parent tag, or an empty React Fragment (<>...</>):
// Correct using React Fragment:
return (
<>
<h1>Title</h1>
<p>Description</p>
</>
);Rule 2: Close Every Single Tag
Tags that don't have children must be self-closed with a trailing slash:
<img src="avatar.jpg" alt="User" />
<input type="text" />
<br />Rule 3: Use className Instead of class
Because class is a reserved keyword in JavaScript (used for OOP classes), JSX uses className for CSS classes:
<div className="profile-container">...</div>Why do we write 'className' instead of 'class' when adding CSS styling in React JSX?
4Dynamic Data with {Curly Braces}
In static HTML, everything you write is literal text. In React, you can open a "window" into JavaScript anywhere in your markup by surrounding code with curly braces { }:
function UserGreeting() {
const name = "Ada Lovelace";
const unreadMessages = 5;
return (
<div>
<h2>Hello, {name}!</h2>
<p>You have {unreadMessages * 2} notifications.</p>
</div>
);
}What will render inside the heading: <h1>{'2 + 3 = ' + (2 + 3)}</h1>?
5Passing Data with Props
What makes components truly powerful is passing different settings (called Props, short for properties) into them, just like passing arguments into a function:
// 1. Child component receives props:
function UserBadge({ name, role }) {
return (
<div className="badge">
<h4>{name}</h4>
<span>{role}</span>
</div>
);
}
// 2. Parent uses it with different data:
<UserBadge name="Alice" role="Admin" />
<UserBadge name="Bob" role="Designer" />6Hands-On: Test Props in Real-Time
Experiment with the component below. Type a new name and role, and toggle the online switch to see the JSX code and rendered component react immediately:
Interactive React Component & Props Explorer
JSX Source Code:
// 1. Define the reusable Component with Props:
function UserBadge({ name, role, isOnline }) {
return (
<div className="badge-card">
<div className="avatar-indicator">
<span className={isOnline ? "dot-online" : "dot-offline"} />
</div>
<h3>{name}</h3>
<p>{role}</p>
<span>{isOnline ? "Active Now" : "Offline"}</span>
</div>
);
}
// 2. Render the Component in your page:
<UserBadge
name="Alex Johnson"
role="Frontend Engineer"
isOnline={true}
/>Rendered Component Output:
Alex Johnson
Frontend Engineer
7Chapter Knowledge Challenge
Test your component, JSX, and props fundamentals before moving to Chapter 2:
Why must custom React components start with an uppercase letter (e.g. <UserCard /> vs <usercard />)?
Can a child component directly modify the props it receives from a parent component?
8Recap
- React apps are built from Components — reusable functions that return UI.
- Component names must begin with a Capital Letter (e.g.
<UserCard />). - JSX rules: Return a single root element (or Fragment
<>...</>), close all tags, and useclassName. - Use curly braces
{ }to embed JavaScript variables, math, and logic inside JSX. - Pass data from parent to child components using Props.
Ready for Chapter 2? Mark this chapter as complete to proceed: