DEV Community

PONVEL M
PONVEL M

Posted on

React Core Concepts Explained

1️⃣

SPA vs MPA | Real DOM vs Virtual DOM | Diffing Algorithm

React is one of the most popular JavaScript libraries for building modern web applications. To truly understand React, it’s important to know how it works internally and why it is faster than traditional approaches.

In this blog, we’ll cover three core concepts:

  • SPA vs MPA
  • Real DOM vs Virtual DOM
  • Diffing Algorithm

SPA (Single Page Application) vs MPA (Multi Page Application)

🔹 What is SPA?

A Single Page Application (SPA) loads only one HTML page from the server. After that, content is updated dynamically without reloading the entire page.

Example: React, Angular, Vue apps

How it works:

  • Browser loads one HTML file
  • JavaScript handles routing
  • Only required data is fetched from the server
  • Page refresh is avoided

Advantages:

  • Faster user experience
  • Smooth navigation
  • Less server load after initial load

Better for complex UI

Disadvantages:

  • Initial load may be slower
  • SEO is more complex (needs SSR or optimization)

🔹 What is MPA?

A Multi Page Application (MPA) loads a new HTML page from the server for every user action.

Example: Traditional websites, e-commerce platforms

How it works:

  • Each click sends a request to the server
  • Server responds with a new page
  • Browser reloads completely

Advantages:

  • SEO friendly
  • Simple architecture
  • Better for content-heavy websites

*Disadvantages:
*

  • Slower navigation
  • Full page reload every time

2️⃣ Real DOM vs Virtual DOM

🔹 Real DOM

The Real DOM is the actual structure of HTML elements rendered in the browser.

*Problem with Real DOM:
*

  • Any change updates the entire DOM tree
  • DOM manipulation is slow
  • Affects performance

Example:
Updating one button may cause the whole page to re-render.

🔹 Virtual DOM

The Virtual DOM is a lightweight JavaScript copy of the Real DOM maintained by React.

How React uses it:

  • Changes are made to the Virtual DOM first
  • React compares old and new Virtual DOM
  • Only necessary updates are applied to Real DOM

Benefits:

  • Faster updates
  • Better performance
  • Efficient UI rendering

3️⃣ Diffing Algorithm in React

🔹 What is Diffing Algorithm?

The Diffing Algorithm **is the logic React uses to **compare the old Virtual DOM with the new Virtual DOM.

This process is also called Reconciliation.

🔹 Why Diffing Algorithm is Needed?

  • Comparing entire DOM trees is expensive
  • React needs a fast way to find changes
  • Diffing minimizes unnecessary updates

🔹 How Diffing Algorithm Works (Simple Rules)

1. Different element types

  • Old element is removed
  • New element is created

2.Same element type

  • React updates only changed attributes
  1. Lists & Keys
  • React uses key to identify elements
  • Helps track changes efficiently

Example:

{items.map(item => (
<li key={item.id}>{item.name}</li>
))}

🔹 Benefits of Diffing Algorithm

  • Improves performance
  • Reduces DOM operations
  • Makes React fast and efficient

✅ Conclusion

Understanding these concepts helps you realize why React is powerful:

  • SPA gives smooth and fast user experience
  • **Virtual DOM **avoids expensive DOM updates
  • Diffing Algorithm ensures only required changes are applied

Together, these make React one of the best tools for modern web development 🚀

Top comments (0)