Before React, most websites were Multi-Page Applications (MPAs). Every time we clicked a link, the browser sent a request to the server, and the server replied with a brand-new HTML file. The screen would flash white, and the entire page would reload.
React popularized the Single Page Application (SPA).
| Feature | MPA (Traditional) | SPA (React) |
|---|---|---|
| Page Load | Reloads entire page on navigation. | Initial load only; updates dynamically. |
| Speed | Slower (renders HTML on server every time). | Faster interactions (renders in browser). |
| User Experience | Choppy (screen flash). | Smooth, app-like. |
| SEO | Easy (Google sees full HTML). | Requires setup (SSR/Next.js) to be seen easily. |
The DOM (Document Object Model) is the browser's structure of the page. It is notoriously slow to manipulate. If we update the real DOM directly (like document.getElementById('root').innerHTML = ...), the browser has to recalculate the layout and repaint the screen, which is expensive.
React solves this with the Virtual DOM.
Following React code looks like a weird mix of HTML and JavaScript. This is JSX. JSX is syntactic sugar over Javascript. JSX is used to write UI in Javascript file making web page content truly dynamic.
const element = <h1>Hello, world!</h1>;Browsers cannot understand JSX. Under the hood, a tool called Babel converts that JSX into standard JavaScript:
Babel is Transpiler (Source to Source Compiler)
Babel takes our modern JSX/React code and translates it into older, standard JavaScript that browsers can understand.
The Transformation:
// Input (JSX)
const element = <h1>Hello</h1>;
// Output (What Babel creates)
const element = React.createElement("h1", null, "Hello");Bundler.bundle.js) so the browser doesn't have to make 500 requests.div or a Fragment (<>...</>).
Why ? Because a JavaScript function can only return one value.
class is a reserved keyword in JavaScript (for making classes), we use className. Similarly, onclick becomes onClick, and tabindex becomes tabIndex.<br> is fine. In JSX, self-closing tags must end with a slash: <br /> or <img />.| Concept | Definition | Key Benefit |
|---|---|---|
| SPA | Single Page Application. Loads once, updates via JS. | Smooth, app-like UX. |
| Virtual DOM | A lightweight JS copy of the real DOM. | Performance (minimizes slow browser repaints). |
| Reconciliation | The process of syncing the Virtual DOM with the Real DOM. | Efficiency (updates only what changed). |
| JSX | Syntax extension for JavaScript that looks like HTML. | Readability (describes UI structure clearly). |
| Babel | A transpiler. | Converts JSX into browser-readable JavaScript. |
Directly manipulating the real DOM is slow because every change can trigger a Reflow (calculating layout/position) and Repaint (drawing pixels).
Layout Thrashing.React.createElement(...) instead of just an HTML string?If a component just returned a string like "<div id='app'>Hello</div>", it would be "dead" text. React couldn't easily track changes within it without re-reading the entire string every time.
React.createElement returns a JavaScript Object.
// It looks roughly like this in memory:
{
type: 'div',
props: { id: 'app', children: 'Hello' },
key: null,
ref: null,
// ...internal React tracking flags
}oldObject.props.id !== newObject.props.id very quickly.