• React is a JavaScript library used for building user interfaces, particularly single-page applications (SPAs) where UI updates efficiently based on user interactions. It provides a declarative and component-based approach to UI development, improving performance and maintainability.

Why Do We Need React?

  • 1.Efficient UI Updates – Traditional DOM manipulation is slow and inefficient, but React updates only necessary parts of the UI using a virtual representation.
  • 2.Component-Based Architecture – React promotes reusable components, making code more modular and maintainable.
  • 3.Declarative Syntax – Instead of imperatively changing the DOM, developers define how the UI should look based on state changes.
  • 4.Performance Optimization – Features like the Virtual DOM and reconciliation process optimize performance.
  • 5.State Management – Built-in state management and integration with libraries like Redux or Context API simplify data handling.

πŸ› οΈ React Workflow Diagram

                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚ React App Starts       β”‚
                β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                β”‚ index.js Renders Root  β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚ JSX Compiled by Babel β”‚
                β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                β”‚ Creates Virtual DOM   β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚ React Fiber Runs       β”‚
                β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                β”‚ Breaks Work in Units   β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚ Diffing Algorithm Runs β”‚
                β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                β”‚ Compares Virtual DOM  β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚ Updates Real DOM       β”‚
                β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                β”‚ Only Changed Parts    β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚ UI Updates & React Components β”‚
                β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                β”‚ State/Props Change β†’ Re-Renderβ”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

// React efficiently updates the UI by using a Virtual DOM, reconciliation process, and Fiber architecture.
 Its component-based design and state management techniques make it a powerful choice for modern web applications.

πŸ“ Notes

  • Virtual DOM: React creates a virtual representation of the UI and updates only necessary changes.
  • Fiber: Enables concurrent rendering for smooth UI updates.
  • Diffing Algorithm: Detects changes between Virtual DOM snapshots and updates only those parts in the real DOM.
  • Event Handling: Uses Synthetic Events for performance optimization.
  • Hooks & Lifecycle: Functional components use Hooks, while class components follow lifecycle methods.

How React Works Under the Hood?

  • Virtual DOM (VDOM)

    • React creates a lightweight copy of the real DOM called the Virtual DOM.
    • When the state of a component changes, React updates the Virtual DOM instead of modifying the real DOM directly.
    • A diffing algorithm (Reconciliation) finds the changes between the previous and new Virtual DOM.
  • Reconciliation Process

    • React determines the minimum number of updates required to sync the real DOM with the Virtual DOM.
    • Uses a β€œdiffing” algorithm to compare the old and new Virtual DOM trees.
    • Efficiently updates only the changed parts of the real DOM.
  • Fiber Architecture

    • React’s Fiber architecture improves reconciliation by breaking rendering work into smaller units, making updates smoother.
    • Supports features like time-slicing and concurrent rendering, leading to better user experience.
  • React Components and JSX

    • Components are reusable, independent pieces of UI built using JavaScript.
    • JSX (JavaScript XML) allows writing HTML-like syntax inside JavaScript, which gets transformed into React elements using Babel.
  • State and Props Management

    • State – A component’s internal data that affects rendering.
    • Props – Data passed from a parent component to a child.
    • React re-renders a component when the state changes, triggering the reconciliation process.
  • Event Handling and Synthetic Events

    • React uses a synthetic event system that wraps native browser events for consistent behavior across different browsers.
    • Improves performance by reducing the number of event listeners attached to the DOM.
  • Hooks (Functional Components' Power-Up)

    • Hooks like useState, useEffect, and useContext allow functional components to have state and lifecycle features without needing class components.
  • React Rendering Lifecycle

    • Mounting – Component is created and added to the DOM.
    • Updating – Component updates due to state or prop changes.
    • Unmounting – Component is removed from the DOM.

Virtual vs real DOM

  • In React, the Virtual DOM is a lightweight copy of the Real DOM that helps improve the performance of web applications. The Real DOM represents the actual structure of the webpage in the browser, and directly updating it can be slow and inefficient, especially when frequent changes occur.

  • React uses the Virtual DOM to handle updates more efficiently. When a change occurs in the application state, React first updates the Virtual DOM instead of the Real DOM. It then compares the updated Virtual DOM with the previous version using a process called β€œdiffing” to identify the differences.

  • After detecting the changes, React updates only the specific parts of the Real DOM that have changed, rather than re-rendering the entire DOM. This approach makes React applications faster and more efficient by minimizing costly operations on the Real DOM.

      State Change in Component
                ↓
        Virtual DOM Updates
                ↓
      Diffing Algorithm Compares Changes
                ↓
   Only Updated Elements are Rendered in Real DOM