SlideShare a Scribd company logo
The Virtual DOM
And how React uses it internally
A first look
Let’s see how is the browser workflow, to render a
web page, before we go into deeper details
Mozilla's Gecko rendering engine main flow
Webkit rendering engine main flow
http://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_general
Collected from the MDN documentation
The basic data flow
1. The rendering engine will start parsing the HTML document and turn the tags to DOM nodes in a
tree called the "content tree”

2. The styling information together with visual instructions in the HTML will be used to create
another tree - the render tree.

3. After the construction of the render tree it goes through a "layout" process. This means giving
each node the exact coordinates where it should appear on the screen.

4. the render tree will be traversed and each node will be painted using the UI backend layer.
http://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_generalData collected from:
Since DOM is represented as a tree structure,
the first render into the DOM is pretty quick!
So, why do we need to use
VDOM?
Mozilla's Gecko rendering engine main flow
Webkit rendering engine main flow
Can you see something in common on the
main browsers workflow after the tree is
processed and attached into the DOM?
Mozilla's Gecko rendering engine main flow
Webkit rendering engine main flow
Can you see some thing in common on the
main browsers workflow, after the tree is
processed e anexada no DOM?
Since DOM is represented as a tree structure, the first
render into the DOM are pretty quick but the changed
element, and its children have to go through Reflow/
Layout stage and then the changes have to be Re-
painted which takes a long time. Therefore the more
items you have to reflow/repaint, the slower your app
becomes.
it's all about rendering
https://www.youtube.com/watch?v=9-ezi9pzdj0
VDOM
"it’s a virtual tree which is kept in the browser memory
and updated in runtime and therefore minimize those
two stages (reflow/repaint) and thereby having a better
performance for a big and complex app”
—Nevinha
How does it work?
When we change something in our Virtual DOM Tree, we get a new
Virtual Tree. The algorithm compares these two trees (old and new),
finds differences and makes only the necessary changes on the DOM,
so at the end it will reflect the virtual DOM.
How is it divided?
1. Node mount representation — React uses JSX and CreateElement
function in order to put it on the virtual tree later. (here) and (here)
2. Mount of the initial virtual tree — It’s possible on web because of
react-dom. (here)
3. Append the VDOM into the DOM — It can be done on the same
function that mounts the virtual tree.
4. For each changes on the virtual tree, the VDOM will compare the
old tree with the new one, in order to see what have been changed
and just re-render the necessary nodes, if and only if it’s necessary.
Node Representation
Basically an element is represented on the VDOM as an object that
contains the following attributes:

• type — The type of the element, which can be a tag name, a function
component (state less) or a class component (state full)
• props or attributes — The attributes of the tag or the custom props
• children — The children that are inside the tag or your component
{
type: "ul",
props: {className: "list"},
children: [
{
type: "li",
props: {},
children: ["item 1"]
},
//{...}
]
}
Stay tuned, it’s react based
https://github.com/facebook/react/blob/master/packages/react/src/ReactElement.js#L312
The next slides will be based on the source code of React.js v16.11.0
which can be founded on the link bellow:
BUT DON’T WORRY ABOUT THE VERSION, IT DOESN'T CHANGE TO
OFFEN 😂
See the node representation that
we’ve just talked few minutes ago?
Mount of the props, if they are custom props, if they
aren’t later react adds then on the element before
append the element into the tree
Also the default props that we define on a
component, they are handled on this function.
Them it returns the a ReactElement ready to use it inside an
existent component or append into the VDOM at the end of the
createElement function
So far…
Right?
Mount of the initial virtual tree
As you already know the basic idea to represent and mount a virtual
node, let’s suppose that we have defined a function called
createElement
Obs: This code doesn’t transpile, because it is pseudo code
As you already have the virtual element creation done, mount
the tree is really easy… you just need to create a function
that get the node representation as argument node and use
recursion to get its virtual children
PS: ignore that we could receive a function or class component on node.type, we will do it later.
Append the VDOM into the DOM
This is the easiest part of the VDOM algorithm, it just uses a function
that takes a component and a container as argument and append the
virtual component, which contains the VTree and append it into the
container.
Obs: This code doesn’t transpile, because it is pseudo code.
Compare changes - DIFF
This is the most important part of the virtual DOM algorithm, it is where
you application get its performance improvement. So basically we
need to write an algorithm, that will compare two virtual trees — old
and new — and make only necessary changes into real DOM.
How react does it?
Actually who is responsible for the DIFF is the react-dom package, on
this link, basically it does diff for the props of a current VTree node,
children and texts.
Let’s see the logic behind the DIFF algorithm that react-dom uses
internally
We will handle the following cases
• There isn’t old node at some place — so node was added and we
need to appendChild(…) that
• There is no new node at some place — thus node was deleted and
we need to removeChild(…)
• Nodes are the same — so we need to go deeper and diff child nodes
We also have the diff of the state and props of a vnode but it won’t be
covered on this presentation.
First things first
We’ll separate a function that does all the magic for us, which we will
call updateElement.
This function will be recursive on the future ;)
There isn’t old node at some place
What is a pretty straightforward operation
There is no new node at some place
Nodes are the same, so we need to go deeper
At this part, we’ll need to adopt the recursion
strategy in order to go deeper into the Tree
First, we’ll create a function that takes two
nodes and see if they are equals
Then we call the changed function inside the update
element function, if the direct children has been
changed, we need to replace the old for the new one
If anything changes between the new and the old
node, it means that we need to go deeper if the new
node is a tag or a component.
The magic is DONE!
You can go beyond
Questions?

More Related Content

What's hot

Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
Knoldus Inc.
 
reactJS
reactJSreactJS
reactJS
Syam Santhosh
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
DivyanshGupta922023
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
Tariqul islam
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
Bethmi Gunasekara
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
ritika1
 
React js
React jsReact js
React js
Rajesh Kolla
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOM
Jimit Shah
 
React workshop
React workshopReact workshop
React workshop
Imran Sayed
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
valuebound
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
namespaceit
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
Ritesh Mehrotra
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
Varun Raj
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
AnmolPandita7
 
React js
React jsReact js
React js
Jai Santhosh
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
Alessandro Valenti
 
React js
React jsReact js
React js
Nikhil Karkra
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to react
kiranabburi
 
Why and How to Use Virtual DOM
Why and How to Use Virtual DOMWhy and How to Use Virtual DOM
Why and How to Use Virtual DOM
Daiwei Lu
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 

What's hot (20)

Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
reactJS
reactJSreactJS
reactJS
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
React js
React jsReact js
React js
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOM
 
React workshop
React workshopReact workshop
React workshop
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
React js
React jsReact js
React js
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
React js
React jsReact js
React js
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to react
 
Why and How to Use Virtual DOM
Why and How to Use Virtual DOMWhy and How to Use Virtual DOM
Why and How to Use Virtual DOM
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 

Similar to The virtual DOM and how react uses it internally

What is virtual dom in react js
What is virtual dom in react jsWhat is virtual dom in react js
What is virtual dom in react js
BOSC Tech Labs
 
Welcome to React & Flux !
Welcome to React & Flux !Welcome to React & Flux !
Welcome to React & Flux !
Ritesh Kumar
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
Divyang Bhambhani
 
Introduction to React JS.pptx
Introduction to React JS.pptxIntroduction to React JS.pptx
Introduction to React JS.pptx
SHAIKIRFAN715544
 
Getting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular DeveloperGetting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular Developer
Fabrit Global
 
Introduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptxIntroduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptx
SHAIKIRFAN715544
 
Learn reactjs, how to code with example and general understanding thinkwik
Learn reactjs, how to code with example and general understanding   thinkwikLearn reactjs, how to code with example and general understanding   thinkwik
Learn reactjs, how to code with example and general understanding thinkwik
Hetaxi patel
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
GDSC UofT Mississauga
 
Web Performance Tips
Web Performance TipsWeb Performance Tips
Web Performance Tips
Ravi Raj
 
Tech Talk on ReactJS
Tech Talk on ReactJSTech Talk on ReactJS
Tech Talk on ReactJS
Atlogys Technical Consulting
 
React patterns
React patternsReact patterns
React patterns
Naimish Verma
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
BOSC Tech Labs
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
Mildain Solutions
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
Ali Sa'o
 
Globant development week / React Redux Rorkshop
Globant development week / React Redux RorkshopGlobant development week / React Redux Rorkshop
Globant development week / React Redux Rorkshop
Globant
 
Vue.js basics
Vue.js basicsVue.js basics
Vue.js basics
patelpareshc
 
JOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and ReduxJOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and Redux
Jordan Open Source Association
 
The complete-beginners-guide-to-react dyrr
The complete-beginners-guide-to-react dyrrThe complete-beginners-guide-to-react dyrr
The complete-beginners-guide-to-react dyrr
AfreenK
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
ShaiAlmog1
 

Similar to The virtual DOM and how react uses it internally (20)

What is virtual dom in react js
What is virtual dom in react jsWhat is virtual dom in react js
What is virtual dom in react js
 
Welcome to React & Flux !
Welcome to React & Flux !Welcome to React & Flux !
Welcome to React & Flux !
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
 
Introduction to React JS.pptx
Introduction to React JS.pptxIntroduction to React JS.pptx
Introduction to React JS.pptx
 
Getting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular DeveloperGetting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular Developer
 
Introduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptxIntroduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptx
 
Learn reactjs, how to code with example and general understanding thinkwik
Learn reactjs, how to code with example and general understanding   thinkwikLearn reactjs, how to code with example and general understanding   thinkwik
Learn reactjs, how to code with example and general understanding thinkwik
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
Web Performance Tips
Web Performance TipsWeb Performance Tips
Web Performance Tips
 
Tech Talk on ReactJS
Tech Talk on ReactJSTech Talk on ReactJS
Tech Talk on ReactJS
 
React patterns
React patternsReact patterns
React patterns
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
Globant development week / React Redux Rorkshop
Globant development week / React Redux RorkshopGlobant development week / React Redux Rorkshop
Globant development week / React Redux Rorkshop
 
Vue.js basics
Vue.js basicsVue.js basics
Vue.js basics
 
JOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and ReduxJOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and Redux
 
The complete-beginners-guide-to-react dyrr
The complete-beginners-guide-to-react dyrrThe complete-beginners-guide-to-react dyrr
The complete-beginners-guide-to-react dyrr
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 

Recently uploaded

Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
christinelarrosa
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Pitangent Analytics & Technology Solutions Pvt. Ltd
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
Enterprise Knowledge
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
saastr
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 

Recently uploaded (20)

Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 

The virtual DOM and how react uses it internally

  • 1. The Virtual DOM And how React uses it internally
  • 2. A first look Let’s see how is the browser workflow, to render a web page, before we go into deeper details Mozilla's Gecko rendering engine main flow Webkit rendering engine main flow http://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_general Collected from the MDN documentation
  • 3. The basic data flow 1. The rendering engine will start parsing the HTML document and turn the tags to DOM nodes in a tree called the "content tree” 2. The styling information together with visual instructions in the HTML will be used to create another tree - the render tree. 3. After the construction of the render tree it goes through a "layout" process. This means giving each node the exact coordinates where it should appear on the screen. 4. the render tree will be traversed and each node will be painted using the UI backend layer. http://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_generalData collected from:
  • 4. Since DOM is represented as a tree structure, the first render into the DOM is pretty quick!
  • 5. So, why do we need to use VDOM?
  • 6. Mozilla's Gecko rendering engine main flow Webkit rendering engine main flow Can you see something in common on the main browsers workflow after the tree is processed and attached into the DOM?
  • 7. Mozilla's Gecko rendering engine main flow Webkit rendering engine main flow Can you see some thing in common on the main browsers workflow, after the tree is processed e anexada no DOM?
  • 8. Since DOM is represented as a tree structure, the first render into the DOM are pretty quick but the changed element, and its children have to go through Reflow/ Layout stage and then the changes have to be Re- painted which takes a long time. Therefore the more items you have to reflow/repaint, the slower your app becomes.
  • 9. it's all about rendering https://www.youtube.com/watch?v=9-ezi9pzdj0
  • 10. VDOM "it’s a virtual tree which is kept in the browser memory and updated in runtime and therefore minimize those two stages (reflow/repaint) and thereby having a better performance for a big and complex app” —Nevinha
  • 11. How does it work? When we change something in our Virtual DOM Tree, we get a new Virtual Tree. The algorithm compares these two trees (old and new), finds differences and makes only the necessary changes on the DOM, so at the end it will reflect the virtual DOM.
  • 12. How is it divided? 1. Node mount representation — React uses JSX and CreateElement function in order to put it on the virtual tree later. (here) and (here) 2. Mount of the initial virtual tree — It’s possible on web because of react-dom. (here) 3. Append the VDOM into the DOM — It can be done on the same function that mounts the virtual tree. 4. For each changes on the virtual tree, the VDOM will compare the old tree with the new one, in order to see what have been changed and just re-render the necessary nodes, if and only if it’s necessary.
  • 13. Node Representation Basically an element is represented on the VDOM as an object that contains the following attributes:
 • type — The type of the element, which can be a tag name, a function component (state less) or a class component (state full) • props or attributes — The attributes of the tag or the custom props • children — The children that are inside the tag or your component
  • 14. { type: "ul", props: {className: "list"}, children: [ { type: "li", props: {}, children: ["item 1"] }, //{...} ] }
  • 15. Stay tuned, it’s react based https://github.com/facebook/react/blob/master/packages/react/src/ReactElement.js#L312 The next slides will be based on the source code of React.js v16.11.0 which can be founded on the link bellow:
  • 16. BUT DON’T WORRY ABOUT THE VERSION, IT DOESN'T CHANGE TO OFFEN 😂
  • 17. See the node representation that we’ve just talked few minutes ago?
  • 18. Mount of the props, if they are custom props, if they aren’t later react adds then on the element before append the element into the tree
  • 19. Also the default props that we define on a component, they are handled on this function.
  • 20. Them it returns the a ReactElement ready to use it inside an existent component or append into the VDOM at the end of the createElement function
  • 22. Mount of the initial virtual tree As you already know the basic idea to represent and mount a virtual node, let’s suppose that we have defined a function called createElement Obs: This code doesn’t transpile, because it is pseudo code
  • 23. As you already have the virtual element creation done, mount the tree is really easy… you just need to create a function that get the node representation as argument node and use recursion to get its virtual children PS: ignore that we could receive a function or class component on node.type, we will do it later.
  • 24. Append the VDOM into the DOM This is the easiest part of the VDOM algorithm, it just uses a function that takes a component and a container as argument and append the virtual component, which contains the VTree and append it into the container.
  • 25. Obs: This code doesn’t transpile, because it is pseudo code.
  • 26. Compare changes - DIFF This is the most important part of the virtual DOM algorithm, it is where you application get its performance improvement. So basically we need to write an algorithm, that will compare two virtual trees — old and new — and make only necessary changes into real DOM.
  • 27. How react does it? Actually who is responsible for the DIFF is the react-dom package, on this link, basically it does diff for the props of a current VTree node, children and texts. Let’s see the logic behind the DIFF algorithm that react-dom uses internally
  • 28. We will handle the following cases • There isn’t old node at some place — so node was added and we need to appendChild(…) that • There is no new node at some place — thus node was deleted and we need to removeChild(…) • Nodes are the same — so we need to go deeper and diff child nodes We also have the diff of the state and props of a vnode but it won’t be covered on this presentation.
  • 29. First things first We’ll separate a function that does all the magic for us, which we will call updateElement. This function will be recursive on the future ;)
  • 30. There isn’t old node at some place What is a pretty straightforward operation
  • 31. There is no new node at some place
  • 32. Nodes are the same, so we need to go deeper At this part, we’ll need to adopt the recursion strategy in order to go deeper into the Tree
  • 33. First, we’ll create a function that takes two nodes and see if they are equals
  • 34. Then we call the changed function inside the update element function, if the direct children has been changed, we need to replace the old for the new one
  • 35. If anything changes between the new and the old node, it means that we need to go deeper if the new node is a tag or a component.
  • 36. The magic is DONE!
  • 37.
  • 38.
  • 39. You can go beyond