Tuesday, May 13, 2025

BIS601 FSD - Difference between Functional and Class Components in ReactJS

 Here's a clear comparison between Class Components and Functional Components in React:


Class vs Functional Components

Feature

Class Component

Functional Component

Syntax

Uses class keyword

Uses plain JS functions or arrow functions

State Management

Uses this.state and this.setState()

Uses useState() hook

Lifecycle Methods

Uses methods like componentDidMount()

Uses useEffect() hook

this keyword

Required (this.state, this.props)

Not required

Hooks support

❌ Not directly supported

✅ Fully supports hooks

Boilerplate code

More verbose

More concise

Performance

Slightly heavier due to binding

Slightly better due to cleaner function closures

Readability

Less readable for beginners

Easier to read and write


When to Use Which

Situation

Recommendation

Writing new React code (modern projects)

✅ Functional

Need to use Hooks (useState, useEffect)

✅ Functional

Maintaining legacy React app

✅ May require Class

Learning lifecycle methods explicitly

🟡 Either, but class for classic syntax


Which Should Be Preferred?

Functional Components are preferred in modern React:

  • React team recommends hooks

  • Cleaner, shorter syntax

  • Better performance patterns

  • Easier to test and reuse logic 

BIS601 FSD - MERN Stack and how it works

 Components of MERN Stack


Component

Role

Description

MongoDB

Database

A NoSQL document-based database that stores data in JSON-like BSON format. Used to persist application data.

Express.js

Web Framework

A lightweight Node.js web application framework used to build RESTful APIs and handle backend routing, middleware, and server logic.

React.js

Frontend Library

A JavaScript library developed by Facebook for building dynamic and responsive user interfaces using a component-based architecture.

Node.js

Runtime Environment

A JavaScript runtime built on Chrome's V8 engine that allows running JavaScript on the server-side. Hosts the Express backend.

How They Work Together (Flow)

  1. React.js – Handles the frontend: displays UI and sends user requests (e.g., form submission).

  2. Express.js – Receives requests from React via HTTP (usually using fetch or axios), handles routes.

  3. Node.js – Runs the server and integrates Express.

  4. MongoDB – Stores and retrieves data requested by Express, which is then sent back to React.

BIS601 FSD - Some Terms associated with ReactJS

 Some terms associated with React


Term

Meaning / Importance

JSX

JavaScript XML – Syntax extension that looks like HTML but compiles to React.createElement() calls.

Component

Reusable building block of UI – can be functional or class-based.

Props

Short for "properties" – inputs to components passed from parent to child. Read-only.

State

Internal data storage in a component. Controls behavior and re-rendering.

Hooks

Functions like useState, useEffect etc., that let you use state and lifecycle features in functional components.

Virtual DOM

In-memory representation of the real DOM. Used by React for efficient UI updates.

Rendering

The process of generating DOM from React components. Can be initial, conditional, or re-rendered.

useEffect

A hook used to handle side effects like API calls, timers, or event listeners.

Lifting State Up

Moving state from a child to a common parent to share data between components.

Controlled Component

A form input whose value is controlled by React state.

Monday, May 12, 2025

BIS601 Full Stack Development - Form Enhancement and Validation

 Form enhancement and validation

A form can be enhanced by styling, having placeholders, validation of input and submitting the form only when all inputs are in order.

The validations used in the below form are name entered should not be empty, email should have @ and . etc.

<!DOCTYPE html>

<html>

<head>

  <title>Simple Form</title>

  <style>

    .error {

      color: red;

    }

  </style>

</head>

<body>


<h3>Simple Form</h3>


<form id="myForm">

  Name: <input type="text" id="name" placeholder=”type name”><br>

  <span id="nameError" class="error"></span><br>


  Email: <input type="text" id="email" placeholder=”type email”><br>

  <span id="emailError" class="error"></span><br>


  <button type="submit">Submit</button>

</form>


<script>

  document.getElementById('myForm').addEventListener('submit', function(e) {

    e.preventDefault(); // stop form from submitting


    // Clear previous errors

    document.getElementById('nameError').textContent = ' ';

    document.getElementById('emailError').textContent = ' ';


    // Get values

    const name = document.getElementById('name').value;

    const email = document.getElementById('email').value;


    let valid = true;


    // Check name

    if (name.trim() === ' ') {

      document.getElementById('nameError').textContent = 'Name is required';

      valid = false;

    }


    // Basic email check (just '@' and '.')

    if (email.indexOf('@') === -1 || email.indexOf('.') === -1) {

      document.getElementById('emailError').textContent = 'Invalid email';

      valid = false;

    }


    if (valid) {

      alert('Form submitted');

    }

  });

</script>


</body>

</html>


BIS601 Full Stack Development - Event Delegation

What is Event Delegation and how does it work?

Event delegation is a technique where a single event listener is added to a parent element instead of each child element individually. It takes advantage of event bubbling in the DOM.

<ul id="myList">

  <li>Item 1</li>

  <li>Item 2</li>

  <li>Item 3</li>

</ul>


// Get the parent element

const list = document.getElementById('myList');

// Attach a single click listener to the parent

list.addEventListener('click', function(event) {

  // Check if a list item (li) was clicked

  if (event.target && event.target.nodeName === 'LI') {

    alert('You clicked: ' + event.target.textContent);

  }

});


BIS601 Full Stack Development - React -Difference between createElement and appendChild

What is the difference between createElement(tag)

and appendChild(node) and how does this relate

to React?

createElement(tag) vs appendChild(node)


Method

Purpose

document.createElement(tag)

Creates a new DOM element (not in the page yet)

parent.appendChild(child)

Adds a node as the last child of another node


Using Both to Create the DOM Tree:

We’ll build:

<form>

  <label></label>

  <ul>

    <li></li>

    <li></li>

  </ul>

</form>


JavaScript Code:

const form = document.createElement('form');

const label = document.createElement('label');

const ul = document.createElement('ul');

const li1 = document.createElement('li');

const li2 = document.createElement('li');


// Build the structure

ul.appendChild(li1);

ul.appendChild(li2);


form.appendChild(label);

form.appendChild(ul);


// Attach to the page (e.g., body)

document.body.appendChild(form);


How Does This Relate to React?

In React, React.createElement() is a virtual version of this DOM API.

For example:

React.createElement('form', null, 

  React.createElement('label', null),

  React.createElement('ul', null,

    React.createElement('li', null),

    React.createElement('li', null)

  )

)


React uses this behind the scenes to build a Virtual DOM, which is then efficiently synced with the real DOM.