Introduction to Components
Introduction to Components explains React component fundamentals, composition, reusable UI, component trees, responsibilities, files, rendering, and maintainable component design.
Components are the fundamental building blocks of React applications.
If you understand components deeply, concepts such as props, state, hooks, composition, reusable UI, and application architecture become much easier.
Learning Objectives
By the end of this lesson, you will understand:
- What a React component is
- Why React applications are built from components
- Functional components
- Component naming conventions
- Returning JSX from components
- Using components inside other components
- Component composition
- Reusability
- Component responsibilities
- Presentational vs container-style thinking
- Component trees
- Parent-child relationships
- Component files
- How React renders components
- Common component mistakes
- Best practices for designing components
What Is a Component?
A component is a reusable piece of UI.
For example, imagine a learning platform:
TechLearningPro│├── Header├── Navigation├── HeroSection├── CourseList│ ├── CourseCard│ ├── CourseCard│ └── CourseCard├── Testimonials└── Footer
Instead of building the entire page as one giant block of JSX, we divide it into meaningful pieces.
Each piece can become a component.
The Component Mental Model
Think of a component as:
Component│├── Input│▼Logic│▼UI
For example:
CourseCard│├── title├── instructor├── price│▼Rendered course card
The component receives information and produces UI.
Your First Component
A simple React component:
function Welcome() {return <h1>Welcome to React</h1>;}
This is a functional component.
It has:
function↓Component name↓()↓JSX
Calling a Component
Components are used like HTML elements.
function Welcome() {return <h1>Welcome to React</h1>;}function App() {return (<div><Welcome /></div>);}
Here:
App↓Welcome↓<h1>
<Welcome /> tells React to render the Welcome component.
Components Look Like HTML
This:
<Welcome />
looks like an HTML element.
But it isn't a built-in HTML element.
It is a React component.
Compare:
<h1>Hello</h1>
with:
<Welcome />
The first is a browser HTML element.
The second represents a React component.
Component Naming
React components should normally use PascalCase.
Correct:
function CourseCard() {return <div>Course</div>;}
Correct:
function UserProfile() {return <div>Profile</div>;}
Avoid:
function courseCard() {// ...}
Use:
CourseCardUserProfileNavigationBarLoginFormProductDetails
PascalCase makes components easy to distinguish from HTML elements.
Why PascalCase?
React uses capitalization to distinguish components from intrinsic HTML elements.
For example:
<div />
means:
HTML div element
Whereas:
<CourseCard />
means:
React component
This distinction is fundamental.
A Component Must Return UI
For example:
function Header() {return (<header><h1>TechLearningPro</h1></header>);}
The return value describes what should appear in the UI.
Components Can Return More Than One Element
This is valid:
function Profile() {return (<div><h2>Jagannath</h2><p>React Developer</p></div>);}
The <div> acts as the parent element.
Fragments
You don't always want to add an unnecessary <div>.
React provides fragments:
function Profile() {return (<><h2>Jagannath</h2><p>React Developer</p></>);}
The empty:
<></>
is a React Fragment.
It allows multiple elements without introducing another DOM element.
Components Can Contain Components
Components become powerful when we combine them.
function Header() {return <header>Header</header>;}function Footer() {return <footer>Footer</footer>;}function App() {return (<><Header /><main><h1>Home</h1></main><Footer /></>);}
The structure becomes:
App│├── Header│├── main