Advanced Web Designing — Study Notes

Unit 02

React fundamentals — organized as exam-ready answers.

3 Questions 5 Marks Each Advanced Web Designing
01
5 Marks Question

What are Components in React? Explain in Detail.

Components are the building blocks of a React application. A component is a reusable piece of UI that defines how a particular part of a web page should appear and behave.

Components divide a large application into small, independent, and reusable parts, making the application easier to develop and maintain.

Types of Components

React mainly has two types of components:

01
Functional Components

A Functional Component is a JavaScript function that returns JSX. It is the commonly used approach for creating components in modern React.

Example
function Welcome() {
    return <h1>Welcome to React</h1>;
}

export default Welcome;

Here, Welcome is a functional component that returns an <h1> element. Functional components can use Hooks such as useState() and useEffect() to manage state and perform other React operations.

02
Class Components

A Class Component is a JavaScript class that extends React.Component and contains a render() method that returns JSX.

Example
import React from "react";

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

export default Welcome;

Here, Welcome is a class component and the render() method returns the UI.

Advantages of Components

Reusability
The same component can be used multiple times.
Modularity
A large application can be divided into smaller components.
Maintainability
Changes can be made to individual components without affecting the entire application.
Independent UI
Each component can manage its own UI and behaviour.
Data Handling
Components can receive data through props and manage data using state.
ConclusionComponents allow React applications to be divided into small, reusable, and independent UI parts. React supports both Functional Components and Class Components, with functional components being commonly used in modern React.
02
5 Marks Question

Explain Form Handling in React

A form in React is used to collect input from users, such as name, email, message, etc. Forms are created using JSX elements such as <form>, <input>, <button>, <textarea>, and <select>.

React handles form data differently from plain HTML by generally using Controlled Components.

Controlled Components

A controlled component is a form element whose value is controlled by React state. The input's value comes from state, and whenever the user changes the input, the state is updated using onChange.

Example
import { useState } from "react";

function SimpleForm() {
    const [name, setName] = useState("");

    return (
        <form>
            <input
                type="text"
                value={name}
                onChange={(e) => setName(e.target.value)}
            />

            <p>You typed: {name}</p>
        </form>
    );
}

Here, value={name} makes the input display the current state value, onChange runs whenever the user types something, and setName() updates the state. React therefore always knows the current value of the input.

Form Submission

The onSubmit event is used to handle form submission.

Example
function SimpleForm() {
    const [name, setName] = useState("");

    const handleSubmit = (e) => {
        e.preventDefault();
        console.log("Submitted:", name);
    };

    return (
        <form onSubmit={handleSubmit}>
            <input
                value={name}
                onChange={(e) => setName(e.target.value)}
            />

            <button type="submit">Submit</button>
        </form>
    );
}

The important statement is e.preventDefault();. Normally, submitting an HTML form reloads the page. e.preventDefault() prevents the default page reload, allowing React to handle the submission using JavaScript.

Handling Multiple Fields

Multiple form fields can be stored in a single state object:

const [formData, setFormData] = useState({
    name: "",
    email: ""
});

const handleChange = (e) => {
    setFormData({
        ...formData,
        [e.target.name]: e.target.value
    });
};

Here, e.target.name identifies which input was changed, e.target.value contains the entered value, and ...formData preserves the existing field values.

Other Form Components

React also supports controlled textarea and select elements using the same value and onChange approach.

ConclusionReact form handling uses state, controlled components, onChange, and onSubmit to keep form data under React's control and process user input without unnecessarily reloading the page.
03
5 Marks Question

Explain Hooks in React

Definition

Hooks are special functions in React that allow Functional Components to use React features such as state, lifecycle methods, context, DOM access, and performance optimization.

All React Hooks start with the word use.

Some Commonly Used Hooks

useState()
Stores and updates component state
useEffect()
Performs side effects such as API calls, timers and event listeners
useContext()
Shares data between components without passing props through every component
useRef()
Accesses DOM elements or stores values without causing re-rendering
useReducer()
Manages complex state and multiple state updates
useCallback()
Remembers a function to improve performance
useMemo()
Remembers the result of an expensive calculation

Example of useState()

useState() is a Hook used to store and update data inside a Functional Component. When the state changes, React automatically re-renders the component and updates the UI.

JSX example
import { useState } from "react";

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

    return (
        <>
            <h1>{count}</h1>

            <button onClick={() => setCount(count + 1)}>
                Increase
            </button>
        </>
    );
}

export default App;

Here, count stores the current state value, setCount is used to update the value, and useState(0) sets the initial value of count to 0. When the button is clicked, setCount(count + 1) updates the state and React re-renders the component.

Advantages of Hooks

Less code
Hooks reduce boilerplate and make components shorter.
Easy to understand
Functional patterns are easier to read and manage.
Reusable logic
Common logic can be shared between components.
Better performance
Hooks help optimize component behaviour and rendering.
No need for Class Components
Functional Components can handle state and lifecycle-like features without classes.
ConclusionHooks make Functional Components more powerful by allowing them to use important React features such as state, side effects, context, references, and performance optimization.