React Deepdive
React Deepdive
React Deepdive
ALPHA TO OMEGA
THIS BOOK
This book is written (typed) by
Ari, who hails from the South
and has keen interests in
Computer Science, Biology,
and Tamil Literature.
Occasionally, he updates his
website, where you can reach
out to him.
https://arihara-sudhan.github.io
⛥ World of React
ReactJS is a JavaScript
library used for building
user interfaces. It was
developed by Facebook
(Meta) and is widely used
for creating single-page
applications (SPAs), where
the entire application is
loaded once, and the
content is dynamically
updated without refreshing
the page. Following is an
example for a Single Page
Application.
It is a web page made with
React. On click a letter, it
won’t refresh the page but
take as to a changed page.
The page never reloads!
Component 3: Vocab
Modular components can
be reused throughout an
application or across
multiple projects. Once a
component is created, it
can be reused wherever
its functionality is needed,
which reduces duplication
of code and speeds up
development. Bugs and
issues are easier to isolate
because they are typically
confined to a specific
component.
React's component-based
architecture promotes a
clear separation of
concerns. Each component
is responsible for one
piece of the UI and handles
both its own rendering
logic and, optionally, state.
Developers can collaborate
more easily. Since
components are self-
contained, multiple
developers can work on
different components
simultaneously.
⛥ Reflow and Repaint
In native web systems, the
actual DOM is manipulated
directly whenever there is
a change in the UI. This
means that each time a
change is made, whether
it's adding, modifying, or
removing elements, the
browser, recalculates the
layout of the page, repaints
the content (if necessary).
Manipulating the DOM
directly can trigger layout
recalculations and screen
redraws (called repaints).
These operations can be
quite expensive, especially
if many changes are
happening frequently or
across many DOM
elements. Accessing and
manipulating the DOM
frequently can be slow
because the DOM is not
optimized for rapid
changes. Each change
requires interaction with
the browser's rendering
engine, which is a costly
process.
Interacting with the
browser's rendering engine
is necessary because it
handles converting the
DOM into what users
visually see. If we're not
careful, native DOM
manipulation can cause
inefficient updates. For
example, making several
changes to the DOM
individually can lead to
performance bottlenecks.
React’s DOM update
process is different from
traditional DOM
manipulation in how it
minimizes the number of
direct updates to the actual
DOM. This is achieved
through the use of the
Virtual DOM, which allows
React to optimize
performance by making
efficient updates, rather
than triggering costly
reflows and repaints for
every change.
React uses an in-memory
Virtual DOM, which is a
lightweight copy of the
actual DOM. When a
component's state or props
change, React updates the
Virtual DOM first, rather
than the real DOM. This
enables React to batch
multiple updates and
compare the changes
more efficiently before
applying them to the actual
DOM.
Whenever a component’s
state or props change,
React automatically re-
renders the component by
updating the Virtual DOM.
This step happens entirely
in memory, so it’s much
faster than working with
the actual DOM. React
uses a process called
reconciliation to determine
what has changed
between the previous
Virtual DOM and the new
Virtual DOM.
React compares the two
versions using a diffing
algorithm, which identifies
only the specific parts of
the DOM that have
changed. Instead of
updating the entire page or
entire component, React
narrows down the changes
to the specific elements or
attributes that need to be
updated. This results in
fewer and more targeted
DOM operations.
Image Courtesy: GeeksforGeeks
React batches updates in a
way that multiple state
changes or DOM updates
are processed together,
rather than one at a time.
This further reduces the
number of direct DOM
manipulations, as React
can group changes and
make them all at once,
avoiding multiple reflows
and repaints. Once React
has determined the
minimal set of changes
needed (based on the
diffing process), it updates
the actual DOM with just
those changes.
This ensures that the
browser’s rendering engine
only recalculates the layout
or repaints for the specific
elements that were
affected, reducing
performance overhead. So,
the two phases are, Render
Phase (or "Reconciliation
Phase") and Commit Phase.
In Render phase, React
updates the Virtual DOM in
memory. When a
component’s state or props
change, React recalculates
the component’s new
Virtual DOM representation
by rendering the
component again. React
then uses its diffing
algorithm to compare the
new Virtual DOM tree with
the previous one, figuring
out the smallest set of
changes that need to be
applied to the actual DOM.
The render phase is purely
in memory.
It doesn’t interact with the
actual DOM yet, so no
reflows, repaints, or any
browser rendering tasks
happen during this phase.
Once the render phase is
complete, React moves to
the commit phase, where it
applies the calculated
changes from the Virtual
DOM to the actual DOM.
React batches the updates
to the actual DOM and
then commits the changes
all at once.
This minimizes the number
of interactions with the real
DOM, which helps improve
performance. During this
phase, React interacts with
the browser’s rendering
engine, triggering layout
recalculations, reflows, and
repaints as necessary
based on the changes.
⛥ REACT ABSTRACTS
React allows developers to
describe what they want
the UI to look like based on
the current state, rather
than managing the UI
updates manually. React
then takes care of the rest.
We describe what the UI
should look like based on
the current state, and React
takes care of updating the
actual DOM to match that
description.
If it is Vanilla JS, there will
be some imperative style in
the code.
⛥ ECOSYSTEM
There are thousands of
packages we probably
don’t need! #JFF
⛥ INSTALL
React requires Node.js and
npm (Node Package
Manager) to be installed on
our machine. Download the
latest version of Node.js
from and install it.
https://nodejs.org