React Tutorial 0/120 lessons ~6 min read Lesson 16

    Introduction to Components

    Introduction to Components explains React component fundamentals, composition, reusable UI, component trees, responsibilities, files, rendering, and maintainable component design.

    Course progress0%
    Focus
    64 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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:

    1. What a React component is
    2. Why React applications are built from components
    3. Functional components
    4. Component naming conventions
    5. Returning JSX from components
    6. Using components inside other components
    7. Component composition
    8. Reusability
    9. Component responsibilities
    10. Presentational vs container-style thinking
    11. Component trees
    12. Parent-child relationships
    13. Component files
    14. How React renders components
    15. Common component mistakes
    16. Best practices for designing components

    What Is a Component?

    A component is a reusable piece of UI.

    For example, imagine a learning platform:

    text
    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:

    text
    Component
    │
    ├── Input
    │
    ▼
    Logic
    │
    ▼
    UI

    For example:

    text
    CourseCard
    │
    ├── title
    ├── instructor
    ├── price
    │
    ▼
    Rendered course card

    The component receives information and produces UI.

    Your First Component

    A simple React component:

    jsx
    function Welcome() {
    return <h1>Welcome to React</h1>;
    }

    This is a functional component.

    It has:

    text
    function
    ↓
    Component name
    ↓
    ()
    ↓
    JSX

    Calling a Component

    Components are used like HTML elements.

    jsx
    function Welcome() {
    return <h1>Welcome to React</h1>;
    }
    function App() {
    return (
    <div>
    <Welcome />
    </div>
    );
    }

    Here:

    text
    App
    ↓
    Welcome
    ↓
    <h1>

    <Welcome /> tells React to render the Welcome component.

    Components Look Like HTML

    This:

    jsx
    <Welcome />

    looks like an HTML element.

    But it isn't a built-in HTML element.

    It is a React component.

    Compare:

    jsx
    <h1>Hello</h1>

    with:

    jsx
    <Welcome />

    The first is a browser HTML element.

    The second represents a React component.

    Component Naming

    React components should normally use PascalCase.

    Correct:

    jsx
    function CourseCard() {
    return <div>Course</div>;
    }

    Correct:

    jsx
    function UserProfile() {
    return <div>Profile</div>;
    }

    Avoid:

    jsx
    function courseCard() {
    // ...
    }

    Use:

    text
    CourseCard
    UserProfile
    NavigationBar
    LoginForm
    ProductDetails

    PascalCase makes components easy to distinguish from HTML elements.

    Why PascalCase?

    React uses capitalization to distinguish components from intrinsic HTML elements.

    For example:

    jsx
    <div />

    means:

    HTML div element

    Whereas:

    jsx
    <CourseCard />

    means:

    React component

    This distinction is fundamental.

    A Component Must Return UI

    For example:

    jsx
    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:

    jsx
    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:

    jsx
    function Profile() {
    return (
    <>
    <h2>Jagannath</h2>
    <p>React Developer</p>
    </>
    );
    }

    The empty:

    jsx
    <>
    </>

    is a React Fragment.

    It allows multiple elements without introducing another DOM element.

    Components Can Contain Components

    Components become powerful when we combine them.

    jsx
    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:

    text
    App
    │
    ├── Header
    │
    ├── main
    │
    └── Footer

    Component Tree

    React applications form a component tree.

    Example:

    text
    App
    │
    ├── Header
    │ ├── Logo
    │ └── Navigation
    │
    ├── Main
    │ ├── Hero
    │ └── CourseList
    │ ├── CourseCard
    │ ├── CourseCard
    │ └── CourseCard
    │
    └── Footer

    This tree is one of the most important concepts in React.

    Parent and Child Components

    If one component renders another component, we can describe the relationship as:

    text
    Parent
    ↓
    Child

    For example:

    jsx
    function Header() {
    return <h1>My Website</h1>;
    }
    function App() {
    return (
    <div>
    <Header />
    </div>
    );
    }

    Here:

    text
    App
    ↓
    Header

    App is the parent.

    Header is the child.

    Why Components?

    Imagine building an e-commerce application without components.

    You might have one huge file containing:

    text
    Header
    Navigation
    Product
    Cart
    Checkout
    Footer
    Login
    Profile
    Orders

    This becomes difficult to:

    text
    Understand
    Maintain
    Test
    Reuse
    Modify

    Components allow us to divide the application into manageable pieces.

    Reusability

    Suppose we have:

    jsx
    function CourseCard() {
    return (
    <div>
    <h2>React Course</h2>
    <p>Learn React</p>
    </div>
    );
    }

    We can render it multiple times:

    jsx
    function App() {
    return (
    <>
    <CourseCard />
    <CourseCard />
    <CourseCard />
    </>
    );
    }

    The same component is reused.

    Later we'll learn how to make each card display different information using props.

    Components Are Like Templates

    Think of:

    jsx
    <CourseCard />

    as a reusable UI template.

    For example:

    text
    CourseCard
    ┌─────────────────────┐
    │ Course Title │
    │ Instructor │
    │ Price │
    │ [Enroll] │
    └─────────────────────┘

    You can create many instances:

    text
    CourseCard
    CourseCard
    CourseCard
    CourseCard

    without duplicating the component's structure.

    Components Can Accept Data

    Components become significantly more useful when they receive data.

    For example:

    jsx
    function CourseCard({ title }) {
    return <h2>{title}</h2>;
    }

    Then:

    jsx
    <CourseCard title="React" />
    <CourseCard title="Java" />
    <CourseCard title="Spring Boot" />

    Result:

    text
    React
    Java
    Spring Boot

    The title is a prop.

    We'll study props in detail in the next lessons.

    Component Inputs

    You can think of components similarly to functions.

    A normal JavaScript function:

    javascript
    function add(a, b) {
    return a + b;
    }

    has:

    text
    Input → Function → Output

    A React component:

    jsx
    function Greeting({ name }) {
    return <h1>Hello {name}</h1>;
    }

    has:

    text
    Props → Component → UI

    This is a very useful mental model.

    Component Logic

    Components can contain JavaScript logic.

    For example:

    jsx
    function UserStatus({ loggedIn }) {
    if (loggedIn) {
    return <p>Welcome back!</p>;
    }
    return <p>Please log in.</p>;
    }

    The component decides what UI to render based on its inputs.

    Components Can Have State

    Components can also maintain their own state.

    jsx
    import { useState } from "react";
    function Counter() {
    const [count, setCount] = useState(0);
    return (
    <button onClick={() => setCount(count + 1)}>
    {count}
    </button>
    );
    }

    Now the component has:

    text
    State
    ↓
    Rendering
    ↓
    User interaction
    ↓
    State update
    ↓
    Re-render

    We'll study state separately.

    Components Are Not Just Visual Boxes

    A component can encapsulate:

    text
    UI
    +
    Behavior
    +
    State
    +
    Events
    +
    Business presentation logic

    For example:

    text
    ShoppingCart
    │
    ├── Cart UI
    ├── Remove item
    ├── Update quantity
    ├── Calculate subtotal
    └── Checkout button

    However, good architecture avoids putting everything into one component.

    Single Responsibility

    A component should generally have a clear responsibility.

    Instead of:

    text
    UserDashboard
    ├── Login
    ├── Payments
    ├── Course Search
    ├── Notifications
    ├── Profile
    ├── Reports
    └── Settings

    you might break it into:

    text
    Dashboard
    ├── DashboardHeader
    ├── CourseSummary
    ├── PaymentSummary
    ├── Notifications
    └── RecentActivity

    Each component has a clearer responsibility.

    Component Size

    There is no universal rule such as:

    Every component must have exactly 50 lines.

    Don't split components purely based on line count.

    Instead look for:

    text
    Repeated UI
    Independent responsibility
    Complex logic
    Independent state
    Reusable behavior
    Meaningful conceptual boundary

    These are stronger reasons to create a component.

    Bad Component Design

    For example:

    jsx
    function Everything() {
    // login logic
    // payment logic
    // course logic
    // notification logic
    // profile logic
    // 1000 lines of JSX
    return (
    // enormous UI
    );
    }

    This is difficult to maintain.

    Better Component Design

    Instead:

    jsx
    function Dashboard() {
    return (
    <>
    <DashboardHeader />
    <CourseSummary />
    <PaymentSummary />
    <Notifications />
    <RecentActivity />
    </>
    );
    }

    Now the application structure communicates its intent.

    Component Composition

    One of React's most important design principles is composition.

    Instead of creating giant components with many configuration options, build smaller components and combine them.

    text
    Small Components
    ↓
    Composition
    ↓
    Larger UI

    Example:

    jsx
    function Page() {
    return (
    <Layout>
    <Header />
    <CourseList />
    <Footer />
    </Layout>
    );
    }

    This is composition.

    Composition vs Duplication

    Bad approach:

    jsx
    function StudentPage() {
    // huge UI
    }
    function TeacherPage() {
    // duplicate huge UI
    }

    Better:

    text
    Shared Layout
    │
    ├── Header
    ├── Sidebar
    └── Content

    Then different pages can compose the shared components.

    Layout Components

    A common component pattern is a layout.

    jsx
    function Layout({ children }) {
    return (
    <>
    <Header />
    <main>
    {children}
    </main>
    <Footer />
    </>
    );
    }

    Then:

    jsx
    function App() {
    return (
    <Layout>
    <Home />
    </Layout>
    );
    }

    Here children represents the content placed inside the layout.

    We'll study children in depth later.

    Components and Files

    A common project structure is:

    text
    src/
    │
    ├── components/
    │ ├── Header.jsx
    │ ├── Footer.jsx
    │ ├── CourseCard.jsx
    │ └── Button.jsx
    │
    ├── pages/
    │ ├── Home.jsx
    │ ├── Courses.jsx
    │ └── Profile.jsx
    │
    ├── App.jsx
    └── main.jsx

    Each component can have its own file.

    One Component Per File

    A common convention is:

    text
    CourseCard.jsx
    ↓
    CourseCard component

    Example:

    jsx
    function CourseCard() {
    return (
    <article>
    <h2>React</h2>
    </article>
    );
    }
    export default CourseCard;

    Then import it:

    jsx
    import CourseCard from "./components/CourseCard";

    And use it:

    jsx
    <CourseCard />

    Exporting Components

    Components need to be exported if another module should import them.

    Default export

    jsx
    export default CourseCard;

    Then:

    jsx
    import CourseCard from "./CourseCard";

    Named export

    jsx
    export function CourseCard() {
    return <div>React</div>;
    }

    Then:

    jsx
    import { CourseCard } from "./CourseCard";

    Both approaches are valid.

    Component Modules

    A component file is simply a JavaScript module containing React code.

    For example:

    text
    CourseCard.jsx
    │
    ├── imports
    ├── component
    ├── styles
    └── export

    This keeps application code organized.

    How React Renders Components

    Suppose:

    jsx
    function App() {
    return <Welcome />;
    }
    function Welcome() {
    return <h1>Hello</h1>;
    }

    Conceptually:

    text
    App
    ↓
    Welcome
    ↓
    <h1>Hello</h1>
    ↓
    DOM

    React builds and manages the UI based on the component tree.

    Component Re-rendering

    When relevant state or props change, React may render a component again.

    For example:

    jsx
    function Counter() {
    const [count, setCount] = useState(0);
    return (
    <button
    onClick={() => setCount(count + 1)}
    >
    {count}
    </button>
    );
    }

    Clicking the button changes state.

    Conceptually:

    text
    count = 0
    ↓
    click
    ↓
    count = 1
    ↓
    React renders updated UI

    A re-render does not mean React blindly recreates the entire browser DOM.

    React determines what needs to change.

    Component Identity

    Consider:

    jsx
    <CourseCard />

    React treats the component as a particular component type in the tree.

    This becomes especially important when dealing with:

    text
    State
    Keys
    Lists
    Conditional rendering
    Component replacement

    We'll explore these concepts later.

    Components and Events

    Components can respond to user actions.

    jsx
    function LoginButton() {
    function handleLogin() {
    console.log("Login clicked");
    }
    return (
    <button onClick={handleLogin}>
    Login
    </button>
    );
    }

    The component contains:

    text
    UI
    +
    Event handler

    Components and Conditional Rendering

    Components can render different UI depending on conditions.

    jsx
    function Status({ isOnline }) {
    return (
    <div>
    {isOnline ? "Online" : "Offline"}
    </div>
    );
    }

    This is one of the most common React patterns.

    Components and Lists

    Components are frequently used with arrays.

    jsx
    const courses = [
    "React",
    "Java",
    "Spring Boot"
    ];

    Then:

    jsx
    function CourseList() {
    return (
    <ul>
    {courses.map(course => (
    <li key={course}>
    {course}
    </li>
    ))}
    </ul>
    );
    }

    Later we'll learn why key is important.

    Components Should Be Predictable

    A well-designed component should ideally behave predictably:

    text
    Same inputs
    ↓
    Predictable output

    For example:

    jsx
    <CourseCard title="React" />

    should consistently represent the React course.

    Avoid components that secretly depend on many unrelated global values.

    Avoid Unnecessary Global Dependencies

    Prefer:

    jsx
    function CourseCard({ title }) {
    return <h2>{title}</h2>;
    }

    over a component that secretly retrieves unrelated information from many places.

    Explicit inputs make components:

    text
    Easier to understand
    Easier to test
    Easier to reuse
    Easier to maintain

    Presentational Components

    A component may primarily focus on UI.

    Example:

    jsx
    function CourseCard({ title, price }) {
    return (
    <article>
    <h2>{title}</h2>
    <p>₹{price}</p>
    </article>
    );
    }

    This component mainly presents data.

    Container-Style Components

    Another component may coordinate data and behavior.

    For example:

    jsx
    function CourseList() {
    // fetch data
    // manage loading
    // handle errors
    return (
    <CourseGrid />
    );
    }

    Historically, React developers often called these container components.

    Modern React architectures don't require a strict container/presentational split, but the underlying idea remains useful:

    Separate responsibilities when complexity requires it.

    Component Hierarchy Example

    Consider an e-commerce page:

    text
    ProductPage
    │
    ├── Header
    │
    ├── ProductDetails
    │ ├── ProductImage
    │ ├── ProductInfo
    │ ├── Price
    │ ├── QuantitySelector
    │ └── AddToCartButton
    │
    ├── Reviews
    │ ├── Review
    │ ├── Review
    │ └── Review
    │
    └── Footer

    This is a component hierarchy.

    Component Boundaries

    A good component boundary often exists where you have:

    A clear concept

    text
    ProductCard

    Repeated UI

    text
    Button

    Independent state

    text
    SearchBox

    Complex behavior

    text
    CheckoutForm

    Reusable layout

    text
    Modal

    These are natural component boundaries.

    Don't Over-Componentize

    There is also a mistake on the opposite side.

    For example:

    jsx
    function LetterH() {
    return <span>H</span>;
    }

    Creating a component for every tiny piece of static markup is usually unnecessary.

    Bad:

    text
    Page
    └── Section
    └── Div
    └── Span
    └── Text

    Components should communicate meaningful concepts.

    Good Componentization

    Instead:

    text
    Page
    ├── Header
    ├── SearchBar
    ├── CourseList
    │ └── CourseCard
    └── Footer

    These names communicate the application's purpose.

    Components and Reuse

    A reusable component should avoid assumptions that prevent reuse.

    Instead of:

    jsx
    function BlueButton() {
    return (
    <button style={{ color: "blue" }}>
    Save
    </button>
    );
    }

    a reusable button might eventually support:

    jsx
    <Button variant="primary">
    Save
    </Button>

    This is where component APIs become important.

    Component API

    A component's API is essentially the way other components interact with it.

    For example:

    jsx
    <Button
    variant="primary"
    disabled={false}
    onClick={handleSave}
    >
    Save
    </Button>

    The component API includes:

    text
    variant
    disabled
    onClick
    children

    Good component APIs are:

    text
    Clear
    Predictable
    Minimal
    Flexible

    Component Coupling

    Avoid tightly coupling a reusable component to a specific application.

    For example:

    jsx
    function Button() {
    // directly calls a specific payment API
    }

    That makes it no longer a generic button.

    Instead:

    jsx
    <Button onClick={handlePayment}>
    Pay
    </Button>

    The parent owns the application-specific behavior.

    Parent Controls, Child Displays

    A useful pattern is:

    text
    Parent
    │
    ├── owns data/behavior
    │
    ▼
    Child
    │
    └── receives what it needs

    For example:

    jsx
    function CoursePage() {
    const course = {
    title: "React"
    };
    return (
    <CourseCard
    title={course.title}
    />
    );
    }

    This creates a clean relationship.

    Component Composition Is a Core React Skill

    Rather than thinking:

    How do I make one giant component?

    Think:

    How can I compose several focused components?

    For example:

    jsx
    function App() {
    return (
    <Layout>
    <Header />
    <Sidebar />
    <MainContent />
    </Layout>
    );
    }

    This is one of the patterns you'll use constantly.

    A Real-World Example

    Imagine a course card:

    jsx
    function CourseCard() {
    return (
    <article>
    <img
    src="/react.png"
    alt="React"
    />
    <h2>React Fundamentals</h2>
    <p>
    Learn modern React.
    </p>
    <button>
    Enroll
    </button>
    </article>
    );
    }

    It has:

    text
    Image
    Title
    Description
    Button

    Later we can extract:

    text
    CourseImage
    CourseTitle
    CourseDescription
    EnrollButton

    But we shouldn't necessarily do that immediately.

    The correct abstraction depends on reuse and complexity.

    Refactor When There Is a Reason

    Don't start with:

    text
    100 tiny components

    and don't create:

    text
    1 giant component

    A practical approach:

    text
    Start simple
    ↓
    Identify repetition
    ↓
    Identify complexity
    ↓
    Extract meaningful components
    ↓
    Reuse them

    This is often more effective.

    Component Best Practices

    1. Use PascalCase

    jsx
    function CourseCard() {}

    2. Give components clear responsibilities

    text
    CourseCard
    LoginForm
    Navigation

    3. Prefer reusable components where reuse exists

    4. Keep APIs simple

    5. Avoid unnecessary global dependencies

    6. Compose components

    7. Don't over-componentize

    8. Keep business logic separate when it becomes complex

    9. Make data dependencies explicit

    10. Use meaningful names

    Common Beginner Mistakes

    Mistake 1 — Lowercase component names

    jsx
    function courseCard() {}

    Prefer:

    jsx
    function CourseCard() {}

    Mistake 2 — Forgetting to return JSX

    jsx
    function Header() {
    <h1>Hello</h1>;
    }

    This doesn't return the JSX.

    Correct:

    jsx
    function Header() {
    return <h1>Hello</h1>;
    }

    Mistake 3 — Treating components as HTML

    Incorrect:

    jsx
    <CourseCard title="React"></CourseCard>

    This can technically work, but self-closing syntax is usually cleaner for components with no children:

    jsx
    <CourseCard title="React" />

    Mistake 4 — Giant components

    text
    App.jsx
    2000 lines

    Break meaningful sections into components.

    Mistake 5 — Too many tiny components

    Don't extract every <div> into a component without a meaningful reason.

    Component Thinking

    When looking at a UI design, start identifying components.

    For example:

    text
    ┌──────────────────────────────┐
    │ Logo Search Profile │
    ├──────────────────────────────┤
    │ │
    │ Welcome to React │
    │ │
    ├───────────┬──────────────────┤
    │ │ │
    │ Sidebar │ Course Card │
    │ │ Course Card │
    │ │ Course Card │
    │ │ │
    └───────────┴──────────────────┘

    You might identify:

    text
    Header
    Logo
    SearchBar
    ProfileMenu
    Hero
    Sidebar
    CourseList
    CourseCard

    This is component thinking.

    Component Tree Exercise

    Consider:

    jsx
    function App() {
    return (
    <>
    <Header />
    <Main />
    <Footer />
    </>
    );
    }
    function Main() {
    return (
    <main>
    <Hero />
    <CourseList />
    </main>
    );
    }

    The component tree is:

    text
    App
    ├── Header
    ├── Main
    │ ├── Hero
    │ └── CourseList
    └── Footer

    Being able to visualize this tree is extremely important.

    Components and the DOM

    Don't confuse:

    text
    Component Tree

    with:

    text
    DOM Tree

    For example:

    jsx
    function Header() {
    return (
    <header>
    <h1>Hello</h1>
    </header>
    );
    }

    The component is:

    text
    Header

    The resulting DOM includes:

    text
    header
    └── h1

    A component is a React abstraction.

    It isn't itself a DOM element.

    Component as a Unit of Organization

    Components help organize:

    text
    Code
    UI
    Behavior
    State
    Testing
    Ownership

    This is why they are foundational to React architecture.

    A More Advanced Mental Model

    Think of a component as:

    text
    Component
    │
    ┌─────────┼─────────┐
    ▼ ▼ ▼
    Props State Context
    │ │ │
    └─────────┼─────────┘
    ▼
    Logic
    │
    ▼
    JSX
    │
    ▼
    React
    │
    ▼
    UI

    Later lessons will explore each of these inputs in depth.

    Interview questions & answers

    Interview preparation

    4 questions grouped by difficulty — expand any card for a model answer and follow-up probe.

    Beginner

    2
    1. 1What Is a React Component?
      Beginner

      Model answer

      A React component is a reusable unit of UI that encapsulates rendering logic and, when needed, state and behavior.

      Components can receive data through props, maintain local state, respond to events, and compose other components.

      React applications are built by composing these components into a component tree.

    2. 2Why Use Components?
      Beginner

      Model answer

      Components allow us to break a complex UI into smaller, focused, reusable units.

      This improves maintainability, readability, testing, and reuse while allowing different parts of the application to evolve independently.

    Intermediate

    1
    1. 3What Is Component Composition?
      Intermediate

      Model answer

      Composition means building larger UI structures by combining smaller components rather than creating large components with many responsibilities.

      React encourages composition because it produces reusable and maintainable component hierarchies.

    Advanced

    1
    1. 4What Makes a Good Component?
      Advanced

      Model answer

      A good component has a clear responsibility, a simple and predictable API, explicit data dependencies, and an appropriate level of reuse.

      I avoid both giant components and unnecessary fragmentation.

      I choose component boundaries based on conceptual responsibility, reuse, state, and complexity.

    Practical Example

    Let's build a small application.

    Header

    jsx
    function Header() {
    return (
    <header>
    <h1>TechLearningPro</h1>
    </header>
    );
    }

    CourseCard

    jsx
    function CourseCard() {
    return (
    <article>
    <h2>React Fundamentals</h2>
    <p>Learn React step by step.</p>
    <button>Enroll</button>
    </article>
    );
    }

    CourseList

    jsx
    function CourseList() {
    return (
    <section>
    <CourseCard />
    <CourseCard />
    <CourseCard />
    </section>
    );
    }

    App

    jsx
    function App() {
    return (
    <>
    <Header />
    <main>
    <h2>Available Courses</h2>
    <CourseList />
    </main>
    </>
    );
    }

    Component tree:

    text
    App
    ├── Header
    └── main
    └── CourseList
    ├── CourseCard
    ├── CourseCard
    └── CourseCard

    This is the foundation of React development.

    What We Will Learn Next

    Now that you understand what components are, the natural progression is:

    text
    Introduction to Components
    ↓
    Creating Components
    ↓
    Component Naming
    ↓
    Component Composition
    ↓
    Props
    ↓
    Passing Data
    ↓
    Component Children
    ↓
    Reusable Components
    ↓
    Component State

    The next important concept is Props because props are how components receive data from their parents.

    Ready to mark this lesson complete?Track your journey across the entire course.