Introduction to Components
A React component is a reusable piece of the interface written as a JavaScript function that returns JSX. A page is built by combining components: a header, a sidebar, a list, each list item, a button. Each one takes inputs called props, can keep its own state, and describes what should appear on screen. Thinking in components is the main idea that makes React apps manageable as they grow.
Your first component
A component is just a function whose name starts with a capital letter. It returns JSX, and you use it like an HTML tag. React calls the function and puts the result into the page.
function TopicCard({ title, level, lessons }) {
return (
<article className="topic-card">
<h3>{title}</h3>
<p>Level: {level}</p>
<p>{lessons} lessons</p>
</article>
);
}
export default function App() {
return (
<main>
<h1>Pick a topic</h1>
<TopicCard title="HTML Basics" level="Beginner" lessons={12} />
<TopicCard title="React Hooks" level="Intermediate" lessons={9} />
</main>
);
}
The same TopicCard function renders twice with different data. That is the core benefit: write the markup and logic once, reuse it with different props.
Props flow down, events flow up
Props are read-only values passed from a parent to a child, like function arguments. A child should never change its props. When a child needs to tell the parent something, such as "this item was clicked", the parent passes a function as a prop and the child calls it. This one-way flow makes it easy to trace where data comes from.
The special children prop holds whatever you place between a component's opening and closing tags, which is useful for wrappers like cards, modals and layouts.
Splitting a page into a component tree
A practical way to start is to sketch the page and draw boxes around parts that repeat or have a clear job. Each box becomes a component, and boxes inside boxes become children. A product page might become:
AppcontainingHeader,ProductListandFooterProductListcontaining manyProductItemcomponentsProductItemcontaining aPriceand anAddToCartButton
Keep each component focused on one job. If a component file becomes long or hard to name, it is usually doing too much and can be split.
Where components live in files
A common convention is one component per file, with the file named after the component, such as TopicCard.jsx, and exported with export default. Small helpers used only by one component can sit in the same file. Styles are often kept next to the component, which the "Adding CSS" lesson covers.
Pitfalls for beginners
Lowercase names are a classic mistake. <topicCard /> is treated as an unknown HTML element, so nothing you wrote renders. Always start component names with an uppercase letter.
Another mistake is defining a component inside another component's body. Each render creates a brand new function, so React sees a different component type every time, throws away its state and rebuilds the DOM. Define components at the top level of the module instead.
A third is returning two sibling elements without a wrapper. A component must return a single root, so wrap siblings in a <div> or an empty fragment <>...</>.
Main ideas
Components are capitalised functions that return JSX. They receive props, can be reused with different data, and nest to form a tree. Keep them small, define them at module level, and pass callbacks down when a child needs to report back.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.