React Components

Topic 2 of 6

React Components

React Components – A Complete Beginner Guide

React is a popular JavaScript library used to build modern and interactive user interfaces.
One of the most important concepts in React is Components.
If you understand components well, you can build powerful web applications easily.

What is a React Component?

A React component is a small, reusable piece of code that controls a part of the user interface (UI).
Think of components like building blocks — you combine them to create a complete application.

For example:
A website may have a Navbar, Footer, Sidebar, and Content section — each can be a separate component.

Types of React Components

1. Functional Components

Functional components are simple JavaScript functions. They are easy to write and widely used.

function Welcome() {
    return <h1>Hello, Welcome to React!</h1>;
}
    

2. Class Components

Class components use ES6 classes. They are older but still used in some projects.

class Welcome extends React.Component {
    render() {
        return <h1>Hello from Class Component</h1>;
    }
}
    

Why Components are Important?

  • Reusable – Write once, use multiple times
  • Organized code – Easy to manage
  • Improves readability
  • Makes development faster

Props in React

Props (short for properties) are used to pass data from one component to another.

function Greeting(props) {
    return <h1>Hello, {props.name}</h1>;
}
    

Usage:

<Greeting name="Rahat" />
    

State in React

State is used to store data that can change over time.
It helps make your components dynamic and interactive.

import { useState } from "react";

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

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

Component Lifecycle (Simple Idea)

Every component goes through different stages:

  • Mounting – Component is created
  • Updating – Component updates when data changes
  • Unmounting – Component is removed

Best Practices for Components

  • Keep components small and simple
  • Use meaningful names
  • Reuse components whenever possible
  • Separate logic and UI clearly

Advantages of React Components

  • Code reusability
  • Better performance
  • Easy debugging
  • Scalable applications

Conclusion

React components are the heart of React applications.
By breaking your UI into small pieces, you can build complex interfaces in a simple and organized way.
Start with functional components and practice using props and state to master React.

FAQs

1. What is a component in React?

A component is a reusable piece of UI that helps build web applications.

2. What is the difference between functional and class components?

Functional components are simple functions, while class components use ES6 classes and have more features (older approach).

3. What are props in React?

Props are used to pass data from one component to another.

4. What is state in React?

State is used to store and manage changing data inside a component.

5. Why are components important?

They make code reusable, organized, and easier to maintain.

6. Can we reuse components?

Yes, components are designed to be reused multiple times.