React Basics: JSX & Components
What is a component?
A component is a reusable piece of UI — a header, a course card, a button. In React, a component is simply a JavaScript function that returns JSX. You write it once and use it as a tag as many times as you like.
// A component is just a function that returns JSX
function Welcome() {
return <h1>Namaste, student!</h1>
}
export default function Page() {
return (
<main>
<Welcome />
<Welcome />
</main>
)
}What is JSX?
JSX lets you write HTML-like markup inside JavaScript. During the build it is converted into normal JavaScript calls that create elements.
JSX rules (very important)
export default function Page() {
return (
<> {/* 1. one parent (Fragment) */}
<h1 className="title">Courses</h1> {/* 2. className, not class */}
<label htmlFor="q">Search</label> {/* 3. htmlFor, not for */}
<input id="q" /> {/* 4. close every tag */}
<img src="/logo.png" alt="Logo" />
<p style={{ color: 'green', fontSize: 18 }}>Open now</p> {/* 5. style is an object */}
<button onClick={() => alert('Hi')}>Click</button> {/* 6. camelCase events */}
</>
)
}- Return one parent element — wrap siblings in
<>...</>(a Fragment) or a<div>. - Use
classNameinstead ofclass. - Use
htmlForinstead offoron labels. - Close every tag:
<img />,<br />,<input />. styletakes a JavaScript object with camelCase keys:{{ fontSize: 18 }}.- Events are camelCase and take a function:
onClick,onChange.
onClick and other events only work in Client Components ('use client'). You will learn this in the Server vs Client lesson — for now just note it.JavaScript inside JSX with { }
Curly braces { } open a "JavaScript window" inside JSX. Put any expression there: variables, maths, function calls, the ternary operator.
export default function Page() {
const name = 'Aman'
const marks = [78, 92, 85]
const total = marks.reduce((a, b) => a + b, 0)
return (
<div>
<p>Student: {name.toUpperCase()}</p>
<p>Total: {total} / 300</p>
<p>Percentage: {((total / 300) * 100).toFixed(1)}%</p>
<p>Result: {total >= 99 ? 'Pass ✅' : 'Fail ❌'}</p>
</div>
)
}You cannot put statements such as if or for directly inside { }. Use the ternary ? : or && instead, and .map() for loops (covered in the Lists lesson).
Make and reuse components in separate files
// components/Header.jsx
export default function Header() {
return (
<header style={{ padding: 16, background: '#4f46e5', color: '#fff' }}>
<strong>EduSite</strong>
</header>
)
}
// components/Footer.jsx
export default function Footer() {
return <footer style={{ padding: 16 }}>© 2026 EduSite</footer>
}
// app/page.jsx
import Header from '@/components/Header'
import Footer from '@/components/Footer'
export default function HomePage() {
return (
<>
<Header />
<main style={{ padding: 16 }}>
<h1>Learn coding the easy way</h1>
</main>
<Footer />
</>
)
}Component tree
Pages are built by putting components inside components. React sees your page as a tree:
Default vs named export
- Default export (
export default function Header) — one main thing per file; import with any name:import Header from .... - Named export — several things per file; import with the exact name in braces.
// components/Buttons.jsx
export function PrimaryButton({ children }) { return <button className="btn">{children}</button> }
export function LinkButton({ children }) { return <button className="btn light">{children}</button> }
// usage — named exports use curly braces
import { PrimaryButton, LinkButton } from '@/components/Buttons'page.jsx, layout.jsx) must use a default export.Common mistakes
function header()lowercase name → nothing appears. UseHeader.- Returning two elements side by side without a parent → "Adjacent JSX elements must be wrapped".
- Writing
style="color:red"→ must bestyle={{ color: 'red' }}. - Forgetting
export defaultinpage.jsx→ Next.js says the page has no default export.
Summary
- A component is a function that returns JSX; names start with a capital letter.
- JSX rules: one parent,
className, close tags, style object, camelCase events. { }runs JavaScript expressions inside JSX.- Keep reusable components in
components/and import them with@/.
Create components/CourseBadge.jsx that shows a green pill with the text "New". Use it three times on the home page.
Frequently Asked Questions
Why must component names start with a capital letter?
Why className instead of class?
What is a Fragment (<>...>)?
Is JSX the same as HTML?
How was this lesson?
Your feedback helps us make the content better.
Write a review or suggestion (optional)
Reviews from readers
Learn with video lessons in easy Hindi
Featured video: RDBMS Part 3 – Class XI Commerce IT
▶ Subscribe – it’s free