React
Components
Web Development
JavaScript
Frontend
UI Development
Programming

What is Components in React?

Deepak Tewatia
August 26, 2025
3 min read

Introduction

React components are like building blocks for your app. They help you break down the user interface into smaller, reusable pieces. Each component can manage its own state and display different parts of the app. This approach helps keep your code clean and easy to understand. Learning about components is key to using React well!

Why Use Components?

Here's the thing: using components makes your code more organized. When you divide your UI into parts, each part can be built and tested separately. This makes it easier to find and fix problems. Plus, you can reuse components in different parts of your app, which saves time and effort.

Types of Components

In React, there are two main types of components: Class Components and Function Components. Let’s break them down:

  • Class Components: These are older and were used before hooks were introduced. They are defined using JavaScript classes and can hold their own state. You might see them in older codebases.
  • Function Components: These are the new standard in React. They are simpler and easier to read. They can use hooks to manage state and side effects, making them very powerful.

How Components Work

Every component in React can receive inputs called props. Props let you pass data from one component to another. This means you can customize how each component looks or behaves based on the data you give it.

Here’s a simple example of a component:

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

In this example, the Greeting component takes a prop called name. When you use it, you can pass any name you want:

<Greeting name="Alice" />
<Greeting name="Bob" />

This will render:

<h1>Hello, Alice!</h1>
<h1>Hello, Bob!</h1>

Managing State in Components

Components can also have their own state. This is data that can change over time. For example, if you have a button that counts clicks, the count is part of the state. You can manage state using the useState hook in function components.

Here’s how you can do it:

import React, { useState } from 'react';

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

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

In this Counter component, we create a state variable called count and a function setCount to update it. Every time you click the button, the count increases by one. This shows how components can react to user actions in real time.

Creating Reusable Components

One of the best things about components is that you can reuse them. If you create a button component, you can use it in many places. Below is an example of a simple button component:

function Button(props) {
  return <button onClick={props.onClick}>{props.label}</button>;
}

You can then use this button component like this:

<Button label="Submit" onClick={handleSubmit} />
<Button label="Cancel" onClick={handleCancel} />

This makes your code DRY, which stands for "Don't Repeat Yourself." Each button can behave differently based on the props you pass in.

Conclusion

React components are essential for building user interfaces. They help you create organized, manageable, and reusable code. Whether you use class components or function components with hooks, understanding how to use components will improve your React skills. So dive in and start creating your own components!