React For Angular Developers!

The Problem:

The one big problem all the frameworks are trying to solve:

The browser's real DOM operations are expensive because they trigger reflows (layout recalculations) and repaints.

When a user types in an input field, the browser processes updates in four main stages:

  1. JavaScript Execution: when an event like oninput triggers, JavaScript modifies the DOM (e.g., updating an <input> field's value).
  2. Style Calculation: If styles depend on the modified element, they might need recalculating for related elements.
  3. Layout (Reflow): If the change affects an element’s size or position, the browser recalculates the layout of affected elements (e.g., increasing a <div> size).
  4. Painting & Compositing: If elements were moved, resized, or changed visually, the browser re-renders them.

How React’s Virtual DOM Optimizes This

Instead of immediately modifying the real DOM, React follows an optimized approach:
  1. JavaScript Execution: Just like before, the input triggers an event (onChange in React).
  2. React Updates the Virtual DOM: Instead of updating the real DOM directly, React modifies a lightweight JavaScript object (Virtual DOM). Real DOM is unchanged for now.
  3. Diffing Algorithm (Reconciliation): React compares the new Virtual DOM with the previous version, and finds the minimal set of changes.
  4. Batch Update & Patch the Real DOM Efficiently: React groups updates together and applies only the necessary changes to the real DOM. This minimizes layout recalculations and repaints.

Advantages of React's Approach:

  • Fewer reflows & repaints, since only the necessary elements get updated.
  • Better performance for complex applications with frequent UI updates.
  • Batching reduces expensive DOM updates.

TLDR

Browser DOM updates are expensive because they trigger reflows (layout recalculations) and repaints when changes occur.

Problem: Directly modifying the DOM causes unnecessary updates, slowing performance.
React’s Solution: Uses a Virtual DOM to track changes, applies only the minimal updates, and batches updates to reduce reflows and repaints.

Benefits:

  • Fewer reflows & repaints: Faster UI updates.
  • Efficient diffing algorithm: Only necessary elements update.
  • Better performance in complex apps with frequent UI changes.

Creating a React app:

To create a React App:
npx create-react-app my-react-app
cd my-react-app
npm start

But this crates JS files, not TS

2 ways to  create TS based react app
  1. Use vite: npm create vite@latest my-app -- --template react-ts
    • cd my-app
    • npm install
    • npm run dev
  2. Regular: npx create-react-app my-app --template typescript
    • cd my-app
    • npm start

Flow:

JSX:
Helps you to write HTML elements inside JavaScript

In this blog, I will go forward with the vite- react app.

Showing Lists:
export const ListComponent = () => {
    const data = ["apple", "orange", "grape"];
    const items = Array.from(data).map(ele => {
        return <p>{ele}</p>
    })
    return <>{items}</>
}

PROPS
Just like how we have @Input() in angular, we have "props" in react
If the same list was being sent as an input:
 <ListComponent data={['apple','orange','grape']} />

export const ListComponent = (props: { data: Array<string> }) => {
    const data = props.data;
    const items = Array.from(data).map((ele, index) => {
        return <p key={index}>{ele}</p>
    })
    return <>{items}</>
}

ONCLICK
export const ListComponent = () => {
    const getItems = () => {
       alert("Hey")
    }
    return <>
                <button onClick={getItems}> Get items </button>
           </>
}
(click) = onClick
getItems() = {getItems}

Project 1: Show list on click of a button. List to come from parent.


Comments

Popular posts from this blog

Inside the JavaScript Memory Box: Visualizing Variables, References, and Copies

React & State Management: All Concepts

React: Communication between components