ReactJS

Components

ReactJS / Components

Components

React Components

Β 

A React component is the fundamental building block of a React application. In this section, we will learn how to create React components and understand their key features.

A React component represents a small part of the user interface on a web page. Its main responsibility is to render the UI and update it whenever its internal data (state) changes. In addition, a component handles user interactions such as clicks, inputs, and other events.

Responsibilities of a React Component

A React component provides the following functionalities:

Initial rendering of the user interface

Handling and managing events

Updating the UI when the internal state changes

These features are achieved using three core concepts:

Props (Properties) – Allow a component to receive input data

Events – Enable the component to handle user interactions

State – Allows the component to store and manage data internally

Types of React Components

Β 

React supports two main types of components:

Function Components

Class Components

Function Components

A function component is a plain JavaScript function that accepts a single argument (props) and returns a React element.

Note: A React element is not a component. A component is composed of one or more elements.

Syntax of a Function Component

Β 

function function_name(argument_name) { Β  return JSX; }

Function components are simple, lightweight, and commonly used for UI rendering.

Class Components

Class components are created using ES6 classes. Every class component must extend React.Component.

Syntax of a Class Component

Β 

class ClassName extends React.Component { Β  render() { Β  Β  Β return

Hello, {this.props.name}

; Β  } }

Class components provide built-in support for state management and lifecycle methods.

Creating a React Component

Β 

React components can be created in two ways:

Function component – Uses JavaScript functions

ES6 class component – Uses JavaScript classes

Key Differences Between Function and Class Components

Function Components

Minimal and simple

Only required to return JSX

Originally did not support state or lifecycle methods

Now support state and lifecycle using hooks (useState, useEffect)

Β 

function Hello() { Β  return

Hello
; }

Class Components

Support state management by default

Have lifecycle methods

Require more boilerplate code

Β 

class Hello extends React.Component { Β  render() { Β  Β  Β return

Hello
; Β  } }

Creating a Class Component Example

Β 

Let us create a class component named ExpenseEntryItem for an expense manager application.

Sample Expense Object

Β 

{ Β  name: "Mango Juice", Β  amount: 30.00, Β  spend_date: "2020-10-10", Β  category: "Food" }

Step 1: Create CSS File

Create ExpenseEntryItem.css inside src/components.

Step 2: Create Component File

Create ExpenseEntryItem.js inside src/components.

Β 

import React from 'react'; import './ExpenseEntryItem.css'; class ExpenseEntryItem extends React.Component { Β  render() { Β  Β  Β return ( Β  Β  Β  Β 

Β  Β  Β  Β  Β  Β 
Item: Mango Juice
Β  Β  Β  Β  Β  Β 
Amount: 30.00
Β  Β  Β  Β  Β  Β 
Spend Date: 2020-10-10
Β  Β  Β  Β  Β  Β 
Category: Food
Β  Β  Β  Β 
Β  Β  Β ); Β  } } export default ExpenseEntryItem;

Step 3: Use the Component in index.js

Β 

import React from 'react'; import { createRoot } from 'react-dom/client'; import ExpenseEntryItem from './components/ExpenseEntryItem'; const container = document.getElementById('root'); const root = createRoot(container); root.render();

Output

Β 

Item: Mango Juice Amount: 30.00 Spend Date: 2020-10-10 Category: FoodΒ 

Creating the Same Component Using CDN

Β 

Β  Β  React ExpenseEntryItem Β 

Β  Β  Β  Β 

Creating a Function Component

Β 

The same component can be written as a function component:

Β 

function ExpenseEntryItem() { Β  return ( Β  Β  Β 

Β  Β  Β  Β 
Item: Mango Juice
Β  Β  Β  Β 
Amount: 30.00
Β  Β  Β  Β 
Spend Date: 2020-10-10
Β  Β  Β  Β 
Category: Food
Β  Β  Β 
Β  ); }

This is sufficient for simple UI components.

Splitting Components (Code Splitting)

Β 

As applications grow, large bundles can slow down performance. Code splitting helps divide code into smaller chunks and loads only what is required.

This technique improves performance by lazy loading components when they are needed, instead of loading everything at once.

Example Without Code Splitting

Β 

import { sub } from './math'; console.log(sub(23, 14));

Example With Code Splitting

Β 

import('./math').then(math => { Β  console.log(math.sub(23, 14)); });

Using import() enables dynamic loading. Tools like Create React App already support code splitting out of the box.

Technology
ReactJS
want to connect with us ?
Contact Us