React Components, Instances, and Elements
React Components, Instances, and Elements
Components
React Components, Instances, and Elements
Components
description of a piece of UI
function that returns a React Element
React Components, Instances, and Elements
Components
description of a piece of UI
function that returns a React Element
React Components, Instances, and Elements
Components
description of a piece of UI
function that returns a React Element
Blueprint / Template
React Components, Instances, and Elements
Components
Component
Instance
React Components, Instances, and Elements
Components
Component
Instance
Instances are created when we "USE" the components
React Components, Instances, and Elements
Components
Component
Instance
Instances are created when we "USE" the components
React internally calls Card()
Actual "physical" manifestation of a component
React Components, Instances, and Elements
Components
Component
Instance
Instances are created when we "USE" the components
React internally calls Card()
Actual "physical" manifestation of a component
has its own state and props
React Components, Instances, and Elements
Components
Component
Instance
Instances are created when we "USE" the components
React internally calls Card()
Actual "physical" manifestation of a component
has its own state and props
has its own lifecycle (be born, live , and die)
React Components, Instances, and Elements
Components
Component
Instance
React Components, Instances, and Elements
Components
Component
Instance
React Element
RETURNS
React Components, Instances, and Elements
Components
Component
Instance
React Element
RETURNS
React Components, Instances, and Elements
Components
Component
Instance
React Element
RETURNS
JSX is converted to React.createElement()
A React element is the result of these function calls
React Components, Instances, and Elements
Components
Component
Instance
React Element
RETURNS
JSX is converted to React.createElement()
A React element is the result of these function calls
information necessary to create DOM elements
React Components, Instances, and Elements
Components
Component
Instance
React Element
RETURNS
React Components, Instances, and Elements
Components
Component
Instance
React Element
RETURNS
INSERT TO DOM
DOM Element
React Components, Instances, and Elements
Components
Component
Instance
React Element
RETURNS
INSERT TO DOM
DOM Element
Actual visual representation of the component instance in the browser
Component A
Component B
Component A
Component B
Component A
Component B
Component
Instance A1
Component
Instance A2
Component
Instance A3
Component
Instance B1
Component A
Component B
Component
Instance A1
Component
Instance A2
Component
Instance A3
Component
Instance B1
Component A
Component B
Component
Instance A1
Component
Instance A2
Component
Instance A3
Component
Instance B1
React
Element
React
Element
React
Element
React
Element
Component A
Component B
Component
Instance A1
Component
Instance A2
Component
Instance A3
Component
Instance B1
React
Element
React
Element
React
Element
React
Element
DOM Element
DOM Element
DOM Element
DOM Element
Component A
Component B
Component
Instance A1
Component
Instance A2
Component
Instance A3
Component
Instance B1
React
Element
React
Element
React
Element
React
Element
DOM Element
DOM Element
DOM Element
DOM Element
User
Interface
on the
Screen
Component A
Component B
Component
Instance A1
Component
Instance A2
Component
Instance A3
Component
Instance B1
React
Element
React
Element
React
Element
React
Element
DOM Element
DOM Element
DOM Element
DOM Element
User
Interface
on the
Screen
how does this process work?
Render is
TRIGGERED
Render is
TRIGGERED
by updating
state somewhere
Render is
TRIGGERED
by updating
state somewhere
Render
Phase
React calls component functions and figures out how DOM should be updated
Render is
TRIGGERED
by updating
state somewhere
Render
Phase
React calls component functions and figures out how DOM should be updated
In React, Rendering is NOT updating the DOM or displyaing elements on the screen
Render is
TRIGGERED
by updating
state somewhere
Render
Phase
React calls component functions and figures out how DOM should be updated
In React, Rendering is NOT updating the DOM or displyaing elements on the screen
Rendering only happens internally inside React, it does not produce visual changes
Render is
TRIGGERED
by updating
state somewhere
Render
Phase
React calls component functions and figures out how DOM should be updated
In React, Rendering is NOT updating the DOM or displyaing elements on the screen
Rendering only happens internally inside React, it does not produce visual changes
Commit
Phase
React actually writes to the DOM, updating, inserting, and deleting elements
Render is
TRIGGERED
by updating
state somewhere
Render
Phase
React calls component functions and figures out how DOM should be updated
In React, Rendering is NOT updating the DOM or displyaing elements on the screen
Rendering only happens internally inside React, it does not produce visual changes
Commit
Phase
React actually writes to the DOM, updating, inserting, and deleting elements
Commonly what we call by "rendering"
Render is
TRIGGERED
by updating
state somewhere
Render
Phase
React calls component functions and figures out how DOM should be updated
In React, Rendering is NOT updating the DOM or displyaing elements on the screen
Rendering only happens internally inside React, it does not produce visual changes
Commit
Phase
React actually writes to the DOM, updating, inserting, and deleting elements
Commonly what we call by "rendering"
Browser
Paint
TWO SITUATIONS
TWO SITUATIONS
1. initial render of application
TWO SITUATIONS
1. initial render of application
2. State is updated in component instances
TWO SITUATIONS
1. initial render of application
2. State is updated in component instances
The render process is triggered for the entire application
TWO SITUATIONS
1. initial render of application
2. State is updated in component instances
The render process is triggered for the entire application
it looks like React only re-renders the component where the state update happens,
but that's not how it works behind the scene
TWO SITUATIONS
1. initial render of application
2. State is updated in component instances
The render process is triggered for the entire application
it looks like React only re-renders the component where the state update happens,
but that's not how it works behind the scene
Renders are not triggered immediately, but scheduled for when the JS engine has some free time.
There is also batching of multiple setState calls in event handlers
Rendering is NOT updating the Screen or DOM
React does not completely discards old view on re-render
Component instances that triggered re-render
React elements
New Virtual DOM
1. Initial Render
1. Initial Render
Virtual DOM: Tree of all React elements created from all instances in the component tree
Cheap and Fast to create multiple tree
Virtual DOM
1. Initial Render
Rerender
Component instances that triggered re-render
React elements
New Virtual DOM
Component instances that triggered re-render
React elements
New Virtual DOM
Reconcilliation + Diffing
with Current Fiber tree before state update
Component instances that triggered re-render
React elements
New Virtual DOM
Reconcilliation + Diffing
with Current Fiber tree before state update
Updated
Fiber tree
Why not update the entire DOM whenever state changes somewhere in the app?
Why not update the entire DOM whenever state changes somewhere in the app?
That would be inefficient and wasteful
1. Writing to the DOM is very expensive
2. Usually only a small part of DOM needs to be updated
Why not update the entire DOM whenever state changes somewhere in the app?
That would be inefficient and wasteful
1. Writing to the DOM is very expensive
2. Usually only a small part of DOM needs to be updated
React reuses as much of the existing DOM as possible
Why not update the entire DOM whenever state changes somewhere in the app?
That would be inefficient and wasteful
1. Writing to the DOM is very expensive
2. Usually only a small part of DOM needs to be updated
React reuses as much of the existing DOM as possible
By using Reconciliation
Why not update the entire DOM whenever state changes somewhere in the app?
That would be inefficient and wasteful
1. Writing to the DOM is very expensive
2. Usually only a small part of DOM needs to be updated
React reuses as much of the existing DOM as possible
By using Reconciliation
Deciding which DOM elements are actually need to be inserted, deleted, or updated to reflect the latest state change
Why not update the entire DOM whenever state changes somewhere in the app?
That would be inefficient and wasteful
1. Writing to the DOM is very expensive
2. Usually only a small part of DOM needs to be updated
React reuses as much of the existing DOM as possible
By using Reconciliation
Deciding which DOM elements are actually need to be inserted, deleted, or updated to reflect the latest state change