Skip to content
Getting StartedChapter 4 / 37Beginner4 min read

Components

Learn what React components are, how to create function components, naming rules, exports and how to combine components to build a UI.

On this page

1. What are components?

Every button, card, navbar and even a whole page can be a component. You build a whole app by joining components like Lego blocks. Modern React uses only function components, and class components are the old way.

2. Why do they matter?

Imagine the same product card appears in 20 places on your site. Without components, the same HTML is copied everywhere, and one design change means editing 20 places.

Without componentsWith components
The same markup copied again and againWrite it once, use it everywhere
Edit many files for one changeChange one component, it updates everywhere
All the UI in one huge fileClean code in small pieces
Hard to testEach component can be tested separately

3. Where are they used?

LevelExample components
Small UI piecesButton, Avatar, Badge, Input
SectionsNavbar, ProductCard, CommentList
PagesHomePage, DashboardPage
LayoutsAppLayout with a Sidebar
LibrariesReady-made components like shadcn/ui and MUI

4. How do they work?

React calls your component function, it returns JSX, and React shows that JSX on the screen. One component uses other components, which creates a component tree.

Component tree
App
Header + ProductList + Footer
Many ProductCard inside ProductList
A Button inside every ProductCard

Component rules

RuleReason
The name starts with a capital letterReact treats a lowercase name as an HTML tag
It returns JSXOr null if there is nothing to show
It is pureThe same input gives the same output, no side effects during render
It is defined at the top levelDo not define a component inside another component

5. Code example

Creating and exporting a component

jsx
// src/components/Button.jsx
export function Button({ label }) {
  return <button className="btn">{label}</button>;
}
jsx
// src/components/ProductCard.jsx
import { Button } from "./Button.jsx";

export function ProductCard({ name, price }) {
  return (
    <article className="card">
      <h3>{name}</h3>
      <p>Rs. {price}</p>
      <Button label="Add to cart" />
    </article>
  );
}

Combining components

jsx
// src/App.jsx
import { ProductCard } from "./components/ProductCard.jsx";

export default function App() {
  return (
    <main>
      <h1>Our Shop</h1>
      <ProductCard name="Keyboard" price={4500} />
      <ProductCard name="Mouse" price={1800} />
    </main>
  );
}

How does the code work?

ItemMeaning
function Button(...)A component is just a function
{ label }Destructuring the props, data comes from the parent
export functionA named export, which another file can import
<Button label="..." />Using a component like a tag
price={4500}A number or JavaScript value in braces
Two ProductCardThe same component, different data, different output

The full details of props are in the Props chapter.

Interview tip

"What is the difference between a component and an element?" A component is a function (ProductCard). An element is the object created when you write JSX (<ProductCard />), which tells React to render this component.

6. Common mistakes

Mistake: Giving a component a lowercase name

Why? React treats button as the HTML button tag, and your component is not rendered.

Better: Always use a PascalCase name, like ProductCard.

Mistake: Defining a component inside another component

Why? A new component is created on every render, its state resets and performance suffers.

Better: Define every component at the top level of the file.

Mistake: Calling the component function yourself

Why? If you write ProductCard(), React does not treat it as a component, and hooks and state do not work properly.

Better: Always use it like a JSX tag, with ProductCard inside angle brackets.

Mistake: Writing the whole app in one component

Why? The file gets long, hard to read and impossible to reuse.

Better: When a part repeats or does a separate job, turn it into a new component.

7. What to learn before and after

Final quick revision

PointDetail
ComponentA function that returns JSX
NamePascalCase, like UserCard
UseLike a tag, <UserCard />
DataFrom the parent through props
StructureA component tree made of components
Old wayClass components, not in new code