Lesson 16 of 60 – React Components
27%

React Components

Components are one of the most important concepts in React. A component is a reusable piece of UI that can contain markup, logic, and behavior. React applications are commonly built by combining many small components together.

Note: Components help divide a large user interface into smaller, reusable, and easier-to-maintain parts.

1. What is a Component?

A React component is a reusable unit of user interface.

function Welcome() {

    return (
        <h1>Welcome to React</h1>
    );

}

Here, Welcome is a React component.

2. Why Use Components?

Components help developers divide a large application into smaller parts.

Large Application
       |
       +-- Header
       +-- Navbar
       +-- Sidebar
       +-- Content
       +-- Footer

Each part can be represented by a separate component.

3. Components Are Reusable

The same component can be used multiple times.

function Button() {

    return (
        <button>Click Me</button>
    );

}

The component can then be rendered in multiple places.

4. Using a Component

A component can be used like a custom HTML element.

function App() {

    return (
        <div>
            <Button />
        </div>
    );

}

Here, Button is a React component.

5. Component Names Start with Capital Letters

React component names should start with an uppercase letter.

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

The capital letter helps React distinguish components from normal HTML elements.

6. Lowercase Names Represent HTML Elements

Lowercase JSX names are generally interpreted as built-in HTML elements.

<div>
    <h1>Hello</h1>
</div>

Custom React components should use names such as Header, StudentCard, or LoginForm.

7. Functional Components

A functional component is a JavaScript function that returns JSX.

function Welcome() {

    return (
        <h1>Welcome</h1>
    );

}

8. Arrow Function Component

Components can also be written using arrow functions.

const Welcome = () => {

    return (
        <h1>Welcome</h1>
    );

};

9. Exporting a Component

A component can be exported so that another file can import it.

function Header() {

    return (
        <h1>My Website</h1>
    );

}

export default Header;

10. Importing a Component

An exported component can be imported into another file.

import Header from "./Header";

function App() {

    return (
        <Header />
    );

}

export default App;

11. Component File Structure

Components are commonly stored in a separate folder.

src/
 |
 +-- components/
 |     +-- Header.jsx
 |     +-- Footer.jsx
 |     +-- Button.jsx
 |
 +-- App.jsx
 +-- main.jsx

12. Header Component

function Header() {

    return (
        <header>
            <h1>My Website</h1>
        </header>
    );

}

export default Header;

The header can be reused throughout the application.

13. Footer Component

function Footer() {

    return (
        <footer>
            <p>Copyright 2026</p>
        </footer>
    );

}

export default Footer;

14. Combining Components

Multiple components can be combined inside another component.

function App() {

    return (
        <div>
            <Header />
            <MainContent />
            <Footer />
        </div>
    );

}

15. Parent and Child Components

When one component renders another component, the first component can be considered the parent and the rendered component the child.

function Parent() {

    return (
        <Child />
    );

}

function Child() {

    return (
        <p>I am a child component.</p>
    );

}

16. Components Can Return Complex JSX

A component can return multiple nested JSX elements.

function StudentCard() {

    return (
        <div className="card">

            <h2>Rahul Kumar</h2>

            <p>
                Course: React
            </p>

            <button>
                View Profile
            </button>

        </div>
    );

}

17. Components Can Contain JavaScript

A component can contain JavaScript variables and logic before its return statement.

function Student() {

    const name = "Rahul";
    const age = 21;

    return (
        <div>
            <h2>{name}</h2>
            <p>Age: {age}</p>
        </div>
    );

}

18. Components and Events

Components can contain event handlers.

function Button() {

    function handleClick() {
        alert("Hello React");
    }

    return (
        <button onClick={handleClick}>
            Click Me
        </button>
    );

}

19. Components and State

Components can use React Hooks such as useState to manage changing data.

import { useState } from "react";

function Counter() {

    const [count, setCount] = useState(0);

    return (
        <button onClick={() => setCount(count + 1)}>
            Count: {count}
        </button>
    );

}

20. Reusing the Same Component

The same component can be rendered multiple times.

function Button() {

    return (
        <button>Click Me</button>
    );

}

function App() {

    return (
        <div>

            <Button />
            <Button />
            <Button />

        </div>
    );

}

21. Component Composition

A large interface can be created by combining smaller components.

App
 |
 +-- Header
 |
 +-- Navbar
 |
 +-- MainContent
 |     |
 |     +-- StudentCard
 |     +-- CourseCard
 |
 +-- Footer

22. Reusable Button Component

function Button() {

    return (
        <button className="btn">
            Submit
        </button>
    );

}

export default Button;

A reusable button can later be enhanced using props.

23. Reusable Card Component

function Card() {

    return (
        <div className="card">

            <h2>React Course</h2>

            <p>
                Learn React step by step.
            </p>

        </div>
    );

}

export default Card;

24. Components Improve Maintainability

Components make applications easier to maintain because related UI and logic can be separated into smaller files.

Large File
    ↓
Split into Components
    ↓
Header
Navbar
Sidebar
Content
Footer

25. Components Improve Reusability

If the same UI appears in multiple places, it can often be converted into a reusable component.

StudentCard
    ↓
Student 1
Student 2
Student 3
Student 4

The same component structure can be reused for different data.

26. Component Folder Organization

src/
 |
 +-- components/
 |     |
 |     +-- Header.jsx
 |     +-- Footer.jsx
 |     +-- Navbar.jsx
 |     +-- Button.jsx
 |     +-- StudentCard.jsx
 |
 +-- pages/
 |     +-- Home.jsx
 |     +-- About.jsx
 |
 +-- App.jsx
 +-- main.jsx

27. Component Naming Best Practices

  • Start component names with an uppercase letter.
  • Use meaningful names.
  • Use PascalCase for component names.
  • Keep names related to the component's purpose.
  • Avoid unclear names such as Box1 or Thing.
StudentCard
LoginForm
NavigationBar
CourseList

28. Simple Component Tree

App
 |
 +-- Header
 |
 +-- Main
 |    |
 |    +-- CourseCard
 |    +-- CourseCard
 |    +-- CourseCard
 |
 +-- Footer

This type of component tree makes the structure of an application easier to understand.

29. Components Best Practices

  • Keep components focused on a clear responsibility.
  • Reuse components when the same UI appears repeatedly.
  • Use meaningful component names.
  • Keep large components manageable.
  • Separate reusable components into their own files when useful.
  • Use props to make components flexible.
  • Use state when a component needs changing data.
  • Avoid unnecessary duplication.
  • Build large interfaces from smaller components.

30. Complete React Components Example

function Header() {

    return (
        <header>
            <h1>My React Website</h1>
        </header>
    );

}

function StudentCard() {

    const name = "Rahul";
    const course = "React";

    return (
        <div className="card">

            <h2>{name}</h2>

            <p>
                Course: {course}
            </p>

            <button>
                View Profile
            </button>

        </div>
    );

}

function Footer() {

    return (
        <footer>
            <p>Learning React</p>
        </footer>
    );

}

function App() {

    return (
        <div>

            <Header />

            <main>
                <StudentCard />
                <StudentCard />
            </main>

            <Footer />

        </div>
    );

}

export default App;

This example demonstrates multiple components, component reuse, nesting, JavaScript variables, JSX, and component composition.

📌 Key Points

  • Components are reusable pieces of a React user interface.
  • Components are commonly created as JavaScript functions.
  • Component names should start with an uppercase letter.
  • Functional components return JSX.
  • Components can be exported and imported between files.
  • Components can contain JavaScript variables and logic.
  • Components can handle events and manage state.
  • A component can render another component.
  • Parent components can contain child components.
  • The same component can be reused multiple times.
  • Large applications can be divided into smaller components.
  • Components make applications easier to organize and maintain.

🧠 Quick Quiz

Question: What is a React component mainly used for?