Explain it simply
Explain it simply
A component is a named function for one piece of UI. Look at any website and you will see parts that repeat: a header, course cards, a footer. Write each part once as a component and reuse it as often as you like, as if it were your own HTML tag, like <CourseCard />.
Components can use other components, so a big App component combines Header, CourseList and Footer, CourseList combines several CourseCards, and so on, forming a tree.
Problem → Why → Solution
The problem: fixing copy-pasted markup everywhere
Say a course page has three cards built by copy-pasting the same HTML. The designer asks for an "Enroll" button in every card, so you add it in three places. With twenty cards on three pages, that is sixty edits, and missing one leaves the cards inconsistent. Meanwhile the single App component grows to hundreds of lines and finding which markup does what becomes hard.
function App() {
return (
<main>
<article className="card">
<h2>React</h2><p>18 lessons</p><button>Enroll</button>
</article>
<article className="card">
<h2>Node.js</h2><p>12 lessons</p><button>Enroll</button>
</article>
<article className="card">
<h2>CSS</h2><p>20 lessons</p>
</article>
</main>
);
}Why is this a problem?
Nothing defines what a card looks like in one place, so every change has to be repeated by hand in every copy. That is exactly how the CSS card ended up without a button. Extract the card into a component and its shape lives in one place; one edit changes every card at once.
Core concept: components, the tree and purity
A component has only three rules: it is a JavaScript function, its name starts with a capital letter (PascalCase), and it returns JSX, or null when it should show nothing. The capital letter matters: if you write <welcomeCard /> in lowercase, React treats it as an HTML tag and never calls your function.
Using a component is not the same as calling the function yourself. Writing <WelcomeCard /> creates an element that means "a WelcomeCard goes here", and React calls the function when it renders. Writing <WelcomeCard /> twice creates two independent instances.
As a project grows, each component usually gets its own file. Write export default function WelcomeCard() in WelcomeCard.jsx and bring it in with import WelcomeCard from "./WelcomeCard.jsx"; in App.jsx. A file can have only one default export but many named exports, such as export function Badge, which you import with braces: import { Badge } from "./Badge.jsx";.
React expects components to be pure. Pure means the same inputs (props and state) always produce the same JSX, and rendering never changes variables, objects or DOM outside the component. You do not control how many times React calls a component; in development StrictMode deliberately calls it twice. So a component that increments an outside counter on every render ends up with wrong numbers. Later lessons show that side effects belong in event handlers instead.
A good rule for how big a component should be is one component, one job. If you cannot name it clearly in two or three words, it is probably doing too much. When a component should show nothing in some situation, it can return null; and the parent needs no changes.
- Spot the repeating pieces of UI
- Make each a PascalCase function
- Parents combine them as <Child />
- React renders the whole tree
Each component does one job, so a change happens in exactly one place.
General pattern
// ChildName.jsx
export default function ChildName() {
return <element>...</element>;
}
// ParentName.jsx
import ChildName from "./ChildName.jsx";
export default function ParentName() {
return <wrapper><ChildName /><ChildName /></wrapper>;
}The child is default-exported from its own file; the parent imports it and uses it as a tag as often as needed. The import path starts with ./, and in Vite you may leave off the .jsx extension.
Example: reusing WelcomeCard twice
This example has two components: WelcomeCard describes one card and App assembles the page. Notice that App never calls WelcomeCard as a function; it only writes it as a tag. Try adding a third <WelcomeCard /> line and watch a third card appear without touching the card itself.
function WelcomeCard() {
return (
<article className="card">
<h2>React Course</h2>
<p>Build the UI from focused components.</p>
</article>
);
}
function App() {
return (
<main>
<h1>ThutaTech Lessons</h1>
<WelcomeCard />
<WelcomeCard />
</main>
);
}Code breakdown
- Line 1
function WelcomeCard() {A plain function with a PascalCase name. It takes no arguments yet because every card shows the same text; the props lesson makes each card different.
- Line 3
<article className="card">The component's root element. className="card" lets one CSS class style every card.
- Line 10
function App() {App is the parent at the top of the tree. Its only job is arranging the pieces; it knows nothing about a card's details.
- Lines 14-15
<WelcomeCard />Two self-closing tags create two WelcomeCard instances. React calls the function twice and produces two <article> elements.
The browser will show a heading and two matching cards.What goes wrong with an impure component
The Cup component below increments an outside guests variable on every render. Rendered once, it shows guests #1, #2 and #3, but in development with StrictMode each component is called twice and you get #2, #4 and #6. Output that depends on how many times rendering happened is the bug. The fix is to pass the number down from the parent as a prop.
let guests = 0;
function Cup() {
guests = guests + 1; // changes a variable outside the component: impure
return <li>Tea for guest #{guests}</li>;
}
export default function TeaList() {
return <ul><Cup /><Cup /><Cup /></ul>;
}In real projects
In real projects
Real projects often keep reusable components such as Button.jsx, Card.jsx and Modal.jsx in a src/components/ folder, and page components that combine them in src/pages/ or src/features/. React does not dictate a folder structure, so follow whatever your team has agreed on.
Knowing when to extract a component is a useful skill. Extract one when markup repeats in more than two places, when a component grows longer than one screen, or when a piece has its own nameable job, such as a search bar or a price summary. Companies with a design system often use a tool like Storybook to show and test each component in isolation. Small, focused components are also far easier to cover with automated tests later in the course.
Common mistakes
Common mistakes
1. Starting a component name in lowercase
Using function welcomeCard() as <welcomeCard /> makes React skip your function and output an unknown HTML tag instead, with a console warning that it "is using incorrect casing. Use PascalCase for React components". Rename it WelcomeCard.
2. Declaring a component inside another component
With function App() { function Card() {...} return <Card />; }, every App render creates a brand-new Card function. React sees a new component type, throws away all of Card's state, and the app gets slower. Declare every component at the top level of a file.
3. A line break right after return
If return sits alone on its line and the JSX starts on the next line without parentheses, JavaScript reads it as return; and gives back undefined. No error appears, the component simply renders nothing, which makes it hard to spot. Keep return ( on one line.
Best practices
Best practices
- Name the file after the component: keep CourseCard in CourseCard.jsx.
- Name components for what they are (CourseCard), not how they look (BlueBox).
- Start with everything in one component and extract pieces once they repeat or grow; splitting too early also hurts readability.
- Never modify outside data during render; same inputs must give the same JSX.
Mini challenge
Mini challenge: split a page layout into components
Write four components, SiteHeader, LessonItem, LessonList and SiteFooter, and combine them in App with a fragment.
- SiteHeader shows a <header> with the site name and a <nav>
- LessonList shows an <h2> and an <ol> containing two <LessonItem />
- SiteFooter shows a year variable through braces
- App combines everything with <>...</> instead of a wrapper div
function SiteHeader() {
// TODO: <header className="site-header"> with <strong> and <nav>
}
function LessonItem() {
// TODO: one <li className="lesson">
}
function LessonList() {
// TODO: <section> with <h2> and an <ol> of two LessonItems
}
function SiteFooter() {
// TODO: const year = ...; render it inside <footer>
}
export default function App() {
// TODO: fragment with SiteHeader, <main><LessonList /></main>, SiteFooter
}Expected: The page shows a header with "ThutaTech" and "Courses | Blog | About", a numbered list under "Today's lessons" with two "Components" items, and the footer "Copyright 2026 ThutaTech".
Show hint
Do not forget return ( ... ) in every component. Using LessonItem inside LessonList adds a level to the tree: App, then LessonList, then LessonItem.
Show solution
function SiteHeader() {
return (
<header className="site-header">
<strong>ThutaTech</strong>
<nav>Courses | Blog | About</nav>
</header>
);
}
function LessonItem() {
return <li className="lesson">Components</li>;
}
function LessonList() {
return (
<section>
<h2>Today's lessons</h2>
<ol>
<LessonItem />
<LessonItem />
</ol>
</section>
);
}
function SiteFooter() {
const year = 2026;
return <footer>Copyright {year} ThutaTech</footer>;
}
export default function App() {
return (
<>
<SiteHeader />
<main>
<LessonList />
</main>
<SiteFooter />
</>
);
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A component is a function with a PascalCase name that returns JSX and is used as <Name />.
- Components nest into a tree, so each piece of UI is defined and changed in one place.
- A file has at most one default export and any number of named exports, which are imported with braces.
- Keep components pure, and never declare a component inside another component.
React Documentation — React