DEV Community

Jenni Juli
Jenni Juli

Posted on

How React Components Work: Real-Time Examples for Beginners

React is one of the most popular JavaScript libraries for building interactive and dynamic user interfaces. Whether you are developing an e-commerce website, a student management system, or a social media application, React components help you build reusable, maintainable, and scalable applications.

In this article, let’s understand how React components work with practical, real-world examples.

What Are React Components?

React components are independent, reusable building blocks of a user interface. Each component handles a specific part of the application, such as a navigation bar, product card, login form, or footer.

Instead of writing the entire application in a single file, developers can divide it into smaller components and combine them to create a complete interface.

For example, an e-commerce website might contain:

Header Component: Displays the website logo and navigation menu.

Product Component: Shows product images, names, and prices.

Cart Component: Displays selected products and cart details.

Footer Component: Contains contact information and useful links.

Each component has a specific responsibility, making the application easier to develop, test, and maintain.

Types of React Components

React components are commonly written as function components. Class components also exist, particularly in older React applications.

  1. Function Components

Function components are JavaScript functions that return React elements using JSX.

Example:

function Welcome() {
return

Welcome to React!

;
}

export default Welcome;

Here, the Welcome component displays a heading. You can reuse it anywhere in your application.

  1. Reusable Components with Props

Props allow you to pass information from a parent component to a child component.

Consider an application that displays details about different students.

function Student(props) {
return (


{props.name}


Course: {props.course}



);
}

function App() {
return (





);
}

export default App;

How it works:

The App component passes student names and course details through props.

The Student component receives those values.

React renders separate student details using the same component.

This approach avoids duplicating the same UI code for every student.

Real-Time Example 1: E-Commerce Product Cards

Imagine you are developing an online shopping website. Each product needs to display its name, price, and an Add to Cart button.

Instead of creating separate code for every product, you can build one reusable product component.

function Product({ name, price }) {
return (


{name}


Price: ₹{price}


Add to Cart

);
}

function App() {
return (






);
}

export default App;

Real-world application: E-commerce platforms can reuse product components to display hundreds of products with consistent layouts. The example demonstrates the UI structure; actual cart functionality requires additional logic.

Real-Time Example 2: Login Form Using State

React components can also respond to user interactions. The useState Hook allows a component to store and update information.

For example, consider a login form that displays the entered username when the user clicks a button.

import { useState } from "react";

function Login() {
const [username, setUsername] = useState("");

return (


Login

  <input
    type="text"
    placeholder="Enter username"
    value={username}
    onChange={(e) => setUsername(e.target.value)}
  />

  <p>Welcome, {username || "Guest"}!</p>
</div>

);
}

export default Login;

How it works:

useState creates a state variable called username.

The input field updates the state whenever the user types.

React re-renders the component with the updated value.

The welcome message changes automatically.

Real-world application: Similar techniques are used in registration forms, search fields, profile pages, and interactive dashboards.

Real-Time Example 3: Building a Student Dashboard

Suppose a training institute needs a dashboard to display student names and their course details. The interface can be divided into smaller components.

function Header() {
return

Student Dashboard

;
}

function StudentCard({ name, course }) {
return (


{name}


Enrolled Course: {course}



);
}

function App() {
return (






);
}

export default App;

In this example, the Header and StudentCard components work together inside the main App component.

Real-world application: This structure can be extended to build student portals, employee management systems, course dashboards, and attendance applications.

Why Are React Components Important?

React components offer several benefits:

Code reusability: Write a component once and use it in multiple places.

Easy maintenance: Update one component without rewriting the entire interface.

Better organization: Divide complex applications into smaller, manageable parts.

Dynamic interfaces: Use props and state to display changing information.

Scalability: Build larger applications by combining smaller components.

Understanding components, props, state, and event handling is an important foundation for developing React applications.

Conclusion

React components make frontend development more structured, reusable, and efficient. From displaying product cards in an e-commerce website to handling login forms and building student dashboards, components help developers create interactive user interfaces with less duplicated code.

For beginners, the best way to understand React components is to practise by building small projects and gradually combining components, props, state, and event handling into complete applications.

If you want to develop practical frontend development skills, consider React JS Training in Chennai at Credo Systemz. Learning through hands-on exercises and real-world projects can help you understand React concepts and apply them to web application development.

Explore React JS training at Credo Systemz to strengthen your frontend development skills and build a foundation for your software development career.

Top comments (0)