SlideShare a Scribd company logo
Something about Lifecycle
Something about Lifecycle
componentWillMount
Something about Lifecycle
componentWillMount
componentDidMount
Something about Lifecycle
componentWillMount
componentDidMount
Mounting
Something about Lifecycle
componentWillMount
componentDidMount
Mounting
render
…
Something about Lifecycle
Something about Lifecycle
componentWillReceiveProps
Something about Lifecycle
componentWillReceiveProps
shouldComponentUpdate
Something about Lifecycle
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
Something about Lifecycle
componentWillReceiveProps
shouldComponentUpdate
Pre-updating
componentWillUpdate
Something about Lifecycle
componentWillReceiveProps
shouldComponentUpdate
Pre-updating
render
…
componentWillUpdate
Something about Lifecycle
Something about Lifecycle
render
Something about Lifecycle
componentDidUpdate
render
…
Something about Lifecycle
componentDidUpdatePost-updating
render
…
Something about Lifecycle
componentDidUpdate
componentWillUnmount
Post-updating
render
…
Something about Lifecycle
componentDidUpdate
componentWillUnmount
Post-updating
render
…
Unmounting
React Lifecycle and Reconciliation
The usage of React is intuitive
The usage of React is intuitive
We re-render whenever state changes
The usage of React is intuitive
We re-render whenever state changes
Re-rendering seems expensive…
The usage of React is intuitive
We re-render whenever state changes
Re-rendering seems expensive…
But the truth is
The usage of React is intuitive
React is FAST!!!
We re-render whenever state changes
Re-rendering seems expensive…
But the truth is
The usage of React is intuitive
React is FAST!!!
We re-render whenever state changes
Re-rendering seems expensive…
But the truth is
WTF???
Batching
Sub-tree Rendering
Batching
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
Batching
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
Sub-tree Rendering
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
Sub-tree Rendering
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
With shouldComponentUpdate, you have control over
whether a component should be re-rendered.
Sub-tree Rendering
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
shouldComponentUpdate
This function is going to be called all the time, so
you want to make sure that it takes less time to
compute the heuristic than the time it would have
taken to render the component, even if re-rendering
was not strictly needed.
React
Reconciliation
React
Reconciliation
a.k.a. Diff Algorithm
Why Another
Tree Diff Algorithm?
Why Another
Tree Diff Algorithm?
O(n3)
Why Another
Tree Diff Algorithm?
O(n3)
1000 nodes would require in the order of
ONE BILLION
comparisons.
2 Basic Assumptions
2 Basic Assumptions
• Two components of the same class will generate
similar trees and two components of different
classes will generate different trees.
2 Basic Assumptions
• Two components of the same class will generate
similar trees and two components of different
classes will generate different trees.
• It is possible to provide a unique key for
elements that is stable across different renders.
Pair-wise Diff
Different Node Types
renderA: <div />
renderB: <span />
=> [removeNode <div />], [insertNode <span />]
Code
renderA: <Header />
renderB: <Content />
=> [removeNode <Header />], [insertNode <Content />]
Code
Pair-wise Diff
DOM Nodes
renderA: <div id="before" />
renderB: <div id="after" />
=> [replaceAttribute id "after"]
Code
renderA: <div style={{color: 'red'}} />
renderB: <div style={{fontWeight: 'bold'}} />
=> [removeStyle color], [addStyle font-weight 'bold']
Code
List-wise Diff
Problematic Case
renderA: <div><span>first</span></div>
renderB: <div><span>first</span><span>second</span></div>
=> [insertNode <span>second</span>]
Code
renderA: <div><span>first</span></div>
renderB: <div><span>second</span><span>first</span></div>
=> [replaceAttribute textContent 'second'], [insertNode
<span>first</span>]
Code
List-wise Diff
Problematic Case
renderA: <div><span>first</span></div>
renderB: <div><span>first</span><span>second</span></div>
=> [insertNode <span>second</span>]
Code
renderA: <div><span>first</span></div>
renderB: <div><span>second</span><span>first</span></div>
=> [replaceAttribute textContent 'second'], [insertNode
<span>first</span>]
Code
Levenshtein Distance
O(n2)
List-wise Diff
Keys
renderA: <div><span key="first">first</span></div>
renderB: <div><span key="second">second</span><span
key="first">first</span></div>
=> [insertNode <span>second</span>]
Code
If you specify a key, React is now able to find insertion,
deletion, substitution and moves in O(n) using a hash table.
Trade-offs
Trade-offs
• The algorithm will not try to match sub-trees of different
components classes. If you see yourself alternating
between two components classes with very similar
output, you may want to make it the same class. In
practice, we haven't found this to be an issue.
Trade-offs
• The algorithm will not try to match sub-trees of different
components classes. If you see yourself alternating
between two components classes with very similar
output, you may want to make it the same class. In
practice, we haven't found this to be an issue.
• If you don't provide stable keys (by using
Math.random() for example), all the sub-trees are
going to be re-rendered every single time. By giving
the users the choice to choose the key, they have the
ability to shoot themselves in the foot.
Where to go from here?
& Acknowledgements
• Official Documentation: https://
facebook.github.io/react/
• React’s diff algorithm: http://
calendar.perfplanet.com/2013/diff/
• React Demystified: http://blog.reverberate.org/
2014/02/react-demystified.html
Thanks

More Related Content

PPTX
learn what React JS is & why we should use React JS .
PPTX
Intro to React
PPTX
What is component in reactjs
PDF
RxJS & Angular Reactive Forms @ Codemotion 2019
PDF
ReactJS presentation
PDF
Javascript Roadmap - The Basics
PPTX
React Hooks
PPTX
React JS - A quick introduction tutorial
learn what React JS is & why we should use React JS .
Intro to React
What is component in reactjs
RxJS & Angular Reactive Forms @ Codemotion 2019
ReactJS presentation
Javascript Roadmap - The Basics
React Hooks
React JS - A quick introduction tutorial

What's hot (20)

PPTX
React js for beginners
PPTX
Introduction to React
PPTX
reactJS
PPTX
React hooks
PPTX
Understanding react hooks
PDF
Introduction to ReactJS
PPTX
ReactJS presentation.pptx
PDF
Next.js Introduction
PDF
NextJS, A JavaScript Framework for building next generation SPA
PDF
ES6 presentation
PPTX
React workshop
PDF
Intro to react native
PPTX
Introduction to react_js
PDF
Introduction into ES6 JavaScript.
PPTX
Introduction to React JS
PDF
TypeScript Introduction
PPTX
React js - The Core Concepts
PDF
React JS - Introduction
PDF
Introduction to React JS
PPT
React js
React js for beginners
Introduction to React
reactJS
React hooks
Understanding react hooks
Introduction to ReactJS
ReactJS presentation.pptx
Next.js Introduction
NextJS, A JavaScript Framework for building next generation SPA
ES6 presentation
React workshop
Intro to react native
Introduction to react_js
Introduction into ES6 JavaScript.
Introduction to React JS
TypeScript Introduction
React js - The Core Concepts
React JS - Introduction
Introduction to React JS
React js
Ad

Viewers also liked (20)

PDF
React Internals
PPTX
React js - Component specs and lifecycle
PPTX
React and Flux life cycle with JSX, React Router and Jest Unit Testing
PDF
Introduce Flux & react in practices (KKBOX)
PDF
React
PDF
Africa_Sabe_CV_eng_june2016
PDF
ΟΑΕΔ, Εγκύκλιος 16435/02.03.2017
DOC
Jitendrasinh Jadon
PPT
Lasersko_zracenje_SiP
PDF
Pharma sp report-1_2017
PPTX
My last vacation lorena
PDF
دليل تربية الدجاج الساسو
PPTX
Sarah T8
PPSX
Portfolio @msemporda
PDF
HRC-Conference-Abstracts-2014
PPTX
PBL GROUP 8
PPTX
Dr kazemian
DOCX
Ralph Dunuan 2015
PDF
Doc McStuffins Episode 229 - part two
DOCX
Fordismo
React Internals
React js - Component specs and lifecycle
React and Flux life cycle with JSX, React Router and Jest Unit Testing
Introduce Flux & react in practices (KKBOX)
React
Africa_Sabe_CV_eng_june2016
ΟΑΕΔ, Εγκύκλιος 16435/02.03.2017
Jitendrasinh Jadon
Lasersko_zracenje_SiP
Pharma sp report-1_2017
My last vacation lorena
دليل تربية الدجاج الساسو
Sarah T8
Portfolio @msemporda
HRC-Conference-Abstracts-2014
PBL GROUP 8
Dr kazemian
Ralph Dunuan 2015
Doc McStuffins Episode 229 - part two
Fordismo
Ad

Similar to React Lifecycle and Reconciliation (20)

PDF
Integrating React.js with PHP projects
PDF
React js
PPTX
PDF
React for Dummies
PDF
Tech Talk on ReactJS
PDF
React - The JavaScript Library for User Interfaces
PDF
Welcome to React & Flux !
PDF
React JS and why it's awesome
PPTX
Adding a modern twist to legacy web applications
PPTX
React JS Workings Exercises Extra Classes
PDF
An Intense Overview of the React Ecosystem
PPTX
React for JavaScipt Introduction and functions
PPTX
ReactJS Code Impact
PDF
Server side rendering with React and Symfony
PDF
FRONTEND DEVELOPMENT WITH REACT.JS
PDF
react hook and wesite making structure ppt
PDF
React & Flux Workshop
PDF
Workshop 19: ReactJS Introduction
PDF
React.js: Beyond the Browser
PDF
theory-slides-vueh3urh4ur4ur4r44oirj4riu4ri
Integrating React.js with PHP projects
React js
React for Dummies
Tech Talk on ReactJS
React - The JavaScript Library for User Interfaces
Welcome to React & Flux !
React JS and why it's awesome
Adding a modern twist to legacy web applications
React JS Workings Exercises Extra Classes
An Intense Overview of the React Ecosystem
React for JavaScipt Introduction and functions
ReactJS Code Impact
Server side rendering with React and Symfony
FRONTEND DEVELOPMENT WITH REACT.JS
react hook and wesite making structure ppt
React & Flux Workshop
Workshop 19: ReactJS Introduction
React.js: Beyond the Browser
theory-slides-vueh3urh4ur4ur4r44oirj4riu4ri

Recently uploaded (20)

PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
top salesforce developer skills in 2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPT
Introduction Database Management System for Course Database
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
System and Network Administraation Chapter 3
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Understanding Forklifts - TECH EHS Solution
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
AI in Product Development-omnex systems
Online Work Permit System for Fast Permit Processing
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
ManageIQ - Sprint 268 Review - Slide Deck
Upgrade and Innovation Strategies for SAP ERP Customers
top salesforce developer skills in 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Introduction Database Management System for Course Database
How Creative Agencies Leverage Project Management Software.pdf
System and Network Administraation Chapter 3
Odoo POS Development Services by CandidRoot Solutions
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Design an Analysis of Algorithms II-SECS-1021-03
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Understanding Forklifts - TECH EHS Solution
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
2025 Textile ERP Trends: SAP, Odoo & Oracle
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How to Migrate SBCGlobal Email to Yahoo Easily
VVF-Customer-Presentation2025-Ver1.9.pptx
AI in Product Development-omnex systems

React Lifecycle and Reconciliation