How Rendering Works: The Render Phase | Lecture 124 | React.JS 🔥

The Coding Classroom
30 Nov 202318:28

Summary

TLDRThis lecture delves into the intricate workings of React's rendering process, unveiling the mechanics behind the scenes. While initially seeming daunting, it promises a fascinating exploration for the curious mind. The virtual DOM, a JavaScript object representation, is demystified, and the reconciliation process is unveiled as the heart of React's efficiency. The Fiber reconciler, with its mutable Fiber tree and asynchronous rendering capabilities, takes center stage. Through practical examples, the script illustrates how React determines what to update, reuse, or remove from the DOM, culminating in a list of effects to be applied during the commit phase.

Takeaways

  • 😃 The lecture covers the complex and fascinating topic of how React performs rendering, which is essential to understanding React's inner workings.
  • 🤔 React does not actually discard and replace the entire component view or DOM during re-renders, contrary to initial explanations.
  • 🌳 The virtual DOM is a tree of React elements representing the entire component tree, which is relatively cheap and fast to create.
  • 🔄 When a component is re-rendered, all of its child components are also re-rendered, regardless of whether their props have changed or not.
  • ⚡ React uses a process called reconciliation to efficiently update the DOM by reusing as much of the existing DOM tree as possible.
  • 🧠 The reconciler, called Fiber, is the engine of React and allows updates without directly touching the DOM.
  • 🌲 The Fiber tree is a mutable data structure that keeps track of component state, props, effects, and more, and is mutated during reconciliation.
  • ⏱️ Fiber can perform work asynchronously, enabling features like Suspense and improving performance by not blocking the JavaScript engine.
  • 🔍 Reconciliation involves diffing, which compares elements step-by-step based on their position in the tree to determine necessary DOM mutations.
  • 📜 The final result of the render phase is an updated Fiber tree and a list of effects (DOM operations) to be executed in the commit phase.

Q & A

  • What is the purpose of this lecture?

    -The lecture aims to explain the inner workings of the rendering process in React, specifically focusing on the render phase and how reconciliation is performed.

  • What is the virtual DOM in React?

    -The virtual DOM, also known as the React element tree, is a tree of all React elements created from all instances in the component tree. It is a JavaScript object representation of the UI, relatively cheap and fast to create.

  • Why does React re-render child components when a parent component is updated?

    -React re-renders child components of an updated component because it doesn't know beforehand whether the update in the parent component will affect the child components or not. By default, React prefers to play it safe and re-render everything.

  • What is the purpose of reconciliation in React?

    -Reconciliation is the process of deciding which DOM elements need to be inserted, deleted, or updated in order to reflect the latest state changes. It allows React to efficiently update the DOM by reusing as much of the existing DOM tree as possible.

  • What is the Fiber reconciler in React?

    -The Fiber reconciler, also known as just 'Fiber,' is the engine of React. It is responsible for performing the reconciliation process and building the Fiber tree, which is an internal tree representing the component instances and DOM elements in the application.

  • What is the Fiber tree in React?

    -The Fiber tree is a special internal tree where, for each component instance and DOM element in the app, there is one so-called 'Fiber.' Fibers are mutable data structures that keep track of things like component state, props, side effects, and hooks. The Fiber tree is never destroyed, but rather mutated during reconciliation.

  • How does the Fiber reconciler handle asynchronous rendering?

    -The Fiber reconciler can perform work asynchronously, splitting the rendering process into chunks, prioritizing tasks, and pausing or resuming work as needed. This allows rendering to pause and resume later, preventing it from blocking the browser's JavaScript engine with long renders.

  • What is the 'diffing' process in React's reconciliation?

    -Diffing is the process of comparing elements step-by-step, based on their position in the tree, to analyze what needs to change between the current Fiber tree and the updated Fiber tree based on the new virtual DOM.

  • What is the 'list of effects' in React's rendering process?

    -The list of effects is a list of DOM mutations (insertions, deletions, or updates) that need to be performed, which is the result of the reconciliation process. This list will be used in the commit phase to actually mutate the DOM.

  • What is the next phase after the render phase in React's rendering process?

    -The next phase after the render phase is the commit phase, where React uses the list of effects generated during the render phase to actually mutate the DOM and apply the changes to the UI.

Outlines

00:00

🧩 The Complexity and Curiosity of React's Rendering Process

This paragraph introduces the complex and confusing nature of React's rendering process, but also highlights its fascinating and interesting aspects. It encourages viewers to approach the topic with an open mind, as understanding the inner workings of React is not essential for using it. The paragraph then revisits the concept of state mechanics and rendering introduced earlier, clarifying that previous explanations were not entirely accurate. It sets the stage for exploring the actual rendering process in this lecture.

05:01

🔄 Triggering Re-renders and the Virtual DOM

This paragraph discusses how re-renders are triggered in React. It explains that during the render phase, React goes through the entire component tree, renders the components that triggered a re-render, and creates an updated virtual DOM (a tree of React elements). The virtual DOM is described as a relatively cheap and fast object representation of the component tree, but the term has been downplayed by the React team in recent documentation. The paragraph then introduces the concept of state updates triggering re-renders and the creation of a new virtual DOM.

10:03

⚡️ The Reconciliation Process and Fiber Architecture

This paragraph introduces the reconciliation process, where the new virtual DOM is reconciled with the current Fiber tree (a mutable data structure representing the component tree). It explains that the reconciliation process results in an updated Fiber tree, which will eventually be used to update the DOM. The paragraph also discusses the Fiber reconciler and its ability to perform work asynchronously, enabling features like Suspense and transitions in React 18. The Fiber tree is described as a linked list data structure that stores component state, props, and side effects.

15:05

🔍 Diffing and DOM Mutations in the Reconciliation Process

This paragraph dives into the reconciliation process using a practical example. It explains how the Fiber reconciler walks through the entire tree, analyzing what needs to change between the current Fiber tree and the updated Fiber tree based on the new virtual DOM. This process of comparing elements step-by-step based on their position in the tree is called diffing. The paragraph then analyzes the updated Fiber tree, highlighting the different types of work (DOM updates, deletions, and unchanged elements) that need to be performed. Finally, it mentions that these DOM mutations will be placed into a list of effects, which will be used in the next phase (the commit phase) to actually mutate the DOM.

Mindmap

Keywords

💡Rendering

Rendering refers to the process of transforming a React component's state and props into a React element tree or virtual DOM. It is a crucial concept in React as it determines how the user interface should be updated based on changes in state or props. The script mentions that rendering is not about updating the screen or DOM directly, but rather about calling component functions and creating updated React elements.

💡Virtual DOM

The virtual DOM (Document Object Model) is a lightweight in-memory representation of the actual DOM tree. It is a tree-like data structure created by React that consists of all the React elements generated from the component instances. The script explains that the virtual DOM is essentially a JavaScript object, making it relatively cheap and fast to create. It serves as an intermediate step before updating the actual DOM, allowing React to efficiently determine the necessary changes.

💡Reconciliation

Reconciliation is the process of determining the minimal set of changes required to update the actual DOM based on the new virtual DOM. It involves comparing the current Fiber tree with the updated Fiber tree created from the new virtual DOM. The script describes reconciliation as deciding exactly which DOM elements need to be inserted, deleted, or updated to reflect the latest state changes.

💡Fiber

Fiber is the name of React's current reconciler, which is responsible for performing the reconciliation process. The script refers to Fiber as the heart or engine of React, enabling it to update the DOM efficiently without directly modifying it. Fiber creates an internal tree called the Fiber tree, where each component instance and DOM element has a corresponding Fiber node.

💡Fiber Tree

The Fiber tree is a special internal tree created by the Fiber reconciler during the initial render of the application. Unlike the virtual DOM, which is recreated on every render, the Fiber tree is a mutable data structure that is mutated during subsequent reconciliation steps. Each Fiber node in the tree contains information about the corresponding component or DOM element, such as its current state, props, side effects, and a queue of work to be performed.

💡Asynchronous Rendering

Asynchronous rendering is a key characteristic of the Fiber reconciler, which allows the rendering process to be split into smaller chunks of work. This means that rendering can be paused, reused, or discarded if no longer valid, without blocking the browser's JavaScript engine. The script mentions that asynchronous rendering enables modern concurrent features like Suspense and transitions in React 18, and helps improve performance in large applications.

💡Diffing

Diffing is the process of comparing elements between the current Fiber tree and the updated Fiber tree based on their position in the tree. The script refers to this as "comparing elements step-by-step" during the reconciliation process. Diffing is an essential part of reconciliation, as it helps determine the minimal set of DOM operations required to update the DOM to match the new virtual DOM.

💡List of Effects

The list of effects is a data structure generated by the reconciliation process that contains a list of DOM operations that need to be performed to update the actual DOM. It includes operations such as inserting, deleting, or updating DOM elements based on the differences identified during the reconciliation process. This list of effects is then used in the subsequent commit phase to mutate the actual DOM.

💡Commit Phase

The commit phase is the final stage of the React rendering process, where the list of effects generated during the reconciliation process is used to actually update the DOM. The script mentions that up until this point, React hasn't written anything to the DOM yet, but it has determined the necessary DOM operations through the reconciliation process. The commit phase is responsible for applying these operations and making the updates visible to the user.

💡State

State is an essential concept in React that represents the internal data of a component. Changes in state trigger re-renders, which in turn trigger the reconciliation process. The script provides an example where updating the `showModal` state in the `App` component triggers a re-render, leading to the creation of a new virtual DOM and subsequent reconciliation.

Highlights

React creates a virtual DOM, which is a JavaScript object tree representing the user interface, to enable efficient updates and reconciliation processes.

When a component is re-rendered, all of its child components are re-rendered as well, regardless of whether their props have changed or not.

React reconciles the new virtual DOM with the current Fiber tree, which is an internal mutable data structure representing the component instances and DOM elements.

The reconciliation process, performed by the Fiber reconciler, determines the necessary DOM operations to update the current DOM with the new state.

The Fiber tree is a special internal tree where each component instance and DOM element has a corresponding Fiber, which stores state, props, side effects, and a queue of work to be done.

The Fiber reconciler can perform work asynchronously, splitting the rendering process into chunks, prioritizing tasks, and pausing or resuming work as needed.

The reconciliation process involves diffing, which compares elements step-by-step based on their position in the tree to determine what needs to change between the current Fiber tree and the updated Fiber tree.

The result of the reconciliation process is a list of DOM operations (effects) that need to be performed in the commit phase to update the actual DOM.

React does not update the entire DOM; it reuses as much of the existing DOM tree as possible and only updates the necessary parts based on the reconciliation process.

The virtual DOM and reconciliation process allow React to efficiently update the user interface without directly touching the DOM, which is an expensive operation.

The Fiber tree is arranged in a linked list structure, with each child Fiber having a link to its parent and siblings, making it easier for React to process the associated work.

The asynchronous rendering enabled by the Fiber reconciler allows modern concurrent features like Suspense and transitions, and prevents blocking the browser's JavaScript engine with long renders.

The reconciliation process involves walking through the entire tree and analyzing exactly what needs to change between the current Fiber tree and the updated Fiber tree based on the new virtual DOM.

The term 'virtual DOM' is a commonly used term, even though the React team has downplayed its meaning, and the official documentation no longer mentions it.

The reconciliation process is performed by the Fiber reconciler, which is considered the engine or heart of React, allowing developers to never touch the DOM directly.

Transcripts

play00:01

‫Welcome back to probably the most complicated

play00:04

‫and most confusing lecture of the entire course.

play00:09

‫But I don't say this to scare you

play00:11

‫because this might also be the most interesting

play00:15

‫and fascinating lecture of the course.

play00:18

‫Now, if you don't immediately understand 100%

play00:22

‫of what I'm gonna teach you here, that's absolutely fine.

play00:26

‫So don't stress about it at all.

play00:29

‫Again, you can use React without knowing any of this stuff

play00:33

‫but if you have a curious mind

play00:36

‫and feel the need to understand how React does what it does,

play00:40

‫then this video is for you.

play00:44

‫Now, before we even start, let's first go back

play00:47

‫to where we first learned about the mechanics of state.

play00:52

‫Remember this diagram?

play00:54

‫Well, back then I told you

play00:56

‫that we can conceptually imagine this

play01:00

‫as a new view being rendered on the screen, so into the DOM.

play01:05

‫However, now we know that this was technically not true

play01:09

‫because rendering is not about the screen or the DOM

play01:13

‫or the view, it's just about calling component functions.

play01:18

‫I also told you that whenever there is a re-render,

play01:22

‫React discards the old component view

play01:25

‫and replaces it with a brand new one.

play01:29

‫However, that's also technically not true.

play01:32

‫So the DOM will actually not be updated

play01:36

‫for the entire component instance.

play01:39

‫So if those things are not true, then let's now learn

play01:43

‫what happens instead and how rendering actually works.

play01:49

‫So, in the previous lecture we talked

play01:52

‫about how renders are triggered.

play01:55

‫In this lecture, we're gonna learn all about

play01:58

‫how renders are actually performed in the render phase.

play02:02

‫So, at the beginning of the render phase

play02:05

‫React will go through the entire component tree,

play02:08

‫take all the component instances that triggered a re-render

play02:12

‫and actually render them, which simply means

play02:15

‫to call the corresponding component functions

play02:18

‫that we have written in our code.

play02:21

‫This will create updated React elements

play02:24

‫which altogether make up the so-called virtual DOM.

play02:29

‫And this is a term that you might have heard before

play02:32

‫and so let's dig a little bit deeper now

play02:35

‫into what the virtual DOM actually is.

play02:39

‫So on the initial render, React will

play02:42

‫take the entire component tree and transform it

play02:46

‫into one big React element which will

play02:48

‫basically be a React element tree like this one.

play02:53

‫And this is what we call the virtual DOM.

play02:57

‫So, the virtual DOM is just a tree

play03:00

‫of all React elements created

play03:02

‫from all instances in the component tree.

play03:06

‫And it's relatively cheap and fast to create a tree

play03:09

‫like this, even if we need many iterations of it

play03:13

‫because in the end it's just a JavaScript object.

play03:17

‫Now, virtual DOM is probably the most hyped

play03:22

‫and most used term when people describe

play03:25

‫what React is and how it works.

play03:28

‫But if we think about it, if the virtual DOM

play03:31

‫is just the simple object,

play03:33

‫it's actually not such a big deal, right?

play03:37

‫And that's why the React team has really

play03:39

‫downplayed the meaning of this name.

play03:42

‫And the official documentation actually no longer

play03:45

‫mentioned the term virtual DOM anywhere.

play03:48

‫But I'm still using this term here

play03:51

‫because everyone still uses it and also

play03:54

‫because it just sounds a bit nicer than React element tree.

play03:59

‫Also, some people confuse the term with the shadow DOM,

play04:04

‫even though it has nothing to do

play04:05

‫with the virtual DOM in React.

play04:08

‫So the shadow DOM is actually just a browser technology

play04:12

‫that is used in stuff like web components.

play04:16

‫But anyway, let's now suppose

play04:18

‫that there is gonna be a state update in component D,

play04:22

‫which will of course trigger a re-render.

play04:26

‫That means that React will call the function

play04:29

‫of component D again and place the new React element

play04:33

‫in a new React element tree.

play04:35

‫So, in a new virtual DOM.

play04:38

‫But now comes the very important part, which is this.

play04:42

‫Whenever React renders a component,

play04:45

‫that render will cause all of its child components

play04:49

‫to be rendered as well.

play04:51

‫And that happens no matter if the props

play04:53

‫that we passed down have changed or not.

play04:57

‫So again, if the updated components

play05:00

‫returns one or more other components,

play05:03

‫those nested components will be re-rendered as well,

play05:07

‫all the way down the component tree.

play05:10

‫This means that if you update the highest component

play05:13

‫in a component tree, in this example component A,

play05:17

‫then the entire application will actually be re-rendered.

play05:22

‫Now, this may sound crazy, but React uses this strategy

play05:27

‫because it doesn't know beforehand whether an update

play05:30

‫in a component will affect the child components or not.

play05:34

‫And so by default, React prefers to play it safe

play05:38

‫and just render everything.

play05:41

‫Also, keep in mind once again that this does not mean

play05:45

‫that the entire DOM is updated.

play05:47

‫It's just a virtual DOM that will be recreated

play05:51

‫which is really not a big problem

play05:53

‫in small or medium sized applications.

play05:57

‫Okay, so with this, we now know what

play06:00

‫this new virtual DOM here means

play06:03

‫and so let's keep moving forward.

play06:06

‫So what happens next is that

play06:08

‫this new virtual DOM that was created

play06:11

‫after the state update will get reconciled

play06:15

‫with the current so-called Fiber tree

play06:18

‫as it exists before the state update.

play06:21

‫Now this reconciliation is done in React's reconciler

play06:26

‫which is called Fiber.

play06:28

‫Now that's why we have a Fiber tree.

play06:31

‫Then the results of this reconciliation process

play06:35

‫is gonna be an updated Fiber tree,

play06:38

‫so a tree that will eventually be used to write to the DOM.

play06:43

‫So this is a high level overview

play06:45

‫of the inputs and the outputs of reconciliation,

play06:50

‫but, of course, now we need to understand

play06:52

‫what reconciliation is and how it works.

play06:58

‫So, you might be wondering why do we even need stuff

play07:01

‫like the virtual DOM, a reconciler and those Fiber trees?

play07:06

‫Why not simply update the entire DOM

play07:09

‫whenever state changes somewhere in the app?

play07:13

‫Well, the answer is simple.

play07:15

‫Remember how we said that creating the virtual DOM

play07:19

‫so the React element tree for the entire app is cheap

play07:23

‫and fast because it's just a JavaScript object?

play07:27

‫Well, writing to the DOM is not cheap and fast.

play07:31

‫It would be extremely inefficient

play07:33

‫and wasteful to always write the entire virtual DOM

play07:38

‫to the actual DOM each time that a render was triggered.

play07:43

‫Also, usually when the state changes somewhere in the app

play07:47

‫only a small portion of the DOM needs

play07:49

‫to be updated and the rest of the DOM

play07:52

‫that is already present can be reused.

play07:56

‫Now, of course, on the initial render

play07:58

‫there is no way around creating the entire DOM from scratch

play08:03

‫but from there on, doing so doesn't make sense anymore.

play08:08

‫So imagine that you have a complex app like udemy.com

play08:13

‫and when you click on some button there

play08:15

‫then showModal is set to true,

play08:18

‫which in turn will then trigger a modal to be shown.

play08:22

‫So in this situation, only the DOM elements

play08:26

‫for that modal need to be inserted into the DOM

play08:30

‫and the rest of the DOM should just stay the same.

play08:34

‫And so that's what React tries to do.

play08:36

‫Whenever a render is triggered,

play08:39

‫React will try to be as efficient as possible

play08:42

‫by reusing as much of the existing DOM tree as possible.

play08:47

‫But that leads us to the next question.

play08:50

‫So how does React actually do that?

play08:53

‫How does it know what changed

play08:55

‫from one render to the next one?

play08:58

‫Well, that's where a process

play09:00

‫called reconciliation comes into play.

play09:04

‫So reconciliation is basically deciding exactly

play09:08

‫which DOM elements need to be inserted, deleted

play09:11

‫or updated in order to reflect the latest state changes.

play09:16

‫So the result of the reconciliation process

play09:19

‫is gonna be a list of DOM operations that are necessary

play09:24

‫to update the current DOM with a new state.

play09:28

‫Now, reconciliation is processed by a reconciler

play09:32

‫and we can say that the reconciler

play09:35

‫really is the engine of React.

play09:37

‫It's like the heart of React.

play09:40

‫So it's this reconciler that allows us

play09:43

‫to never touch the DOM directly

play09:46

‫and instead simply tell React what the next snapshot

play09:49

‫of the UI should look like based on state.

play09:53

‫And as I mentioned before, the current reconciler

play09:56

‫in React is called Fiber, and this is how it works.

play10:02

‫So, during the initial render of the application

play10:06

‫Fiber takes the entire React element tree,

play10:09

‫so the virtual DOM, and based on it

play10:13

‫builds yet another tree which is the Fiber tree.

play10:17

‫The Fiber tree is a special internal tree where

play10:21

‫for each component instance and DOM element in the app,

play10:25

‫there is one so-called Fiber.

play10:28

‫Now, what's special about this tree

play10:30

‫is that unlike React elements in the virtual DOM,

play10:34

‫Fibers are not recreated on every render.

play10:38

‫So the Fiber tree is never destroyed.

play10:41

‫Instead, it's a mutable data structure

play10:44

‫and once it has been created during the initial render,

play10:47

‫it's simply mutated over and over again

play10:50

‫in future reconciliation steps.

play10:53

‫And this makes Fibers the perfect place for keeping track

play10:57

‫of things like the current component state, props,

play11:01

‫side effects, list of used hooks and more.

play11:06

‫So the actual state and props of any component instance

play11:11

‫that we see on the screen are internally stored

play11:14

‫inside the corresponding Fiber in the Fiber tree.

play11:18

‫Now, each Fiber also contains a queue of work to do

play11:22

‫like updating state, updating refs,

play11:25

‫running registered side effects,

play11:28

‫performing DOM updates and so on.

play11:31

‫This is why a Fiber is also defined as a unit of work.

play11:36

‫Now, if we take a quick look at the Fiber tree

play11:39

‫we will see that the Fibers are arranged in a different way

play11:43

‫than the elements in the React element tree.

play11:46

‫So instead of a normal parent-child relationship,

play11:50

‫each first child has a link to its parent

play11:53

‫and all the other children then have a link

play11:56

‫to their previous sibling.

play11:58

‫And this kind of structure is called a linked list

play12:02

‫and it makes it easier for React to process the work

play12:05

‫that is associated with each Fiber.

play12:09

‫We also see that both trees include not only React elements

play12:13

‫or components, but also regular DOM elements,

play12:17

‫such as the h3 and button elements in this example.

play12:22

‫So both trees really are a complete representation

play12:26

‫of the entire DOM structure, not just of React components.

play12:31

‫Now going back to the idea that Fibers are units of work,

play12:36

‫one extremely important characteristic

play12:39

‫of the Fiber reconciler is that work

play12:42

‫can be performed asynchronously.

play12:45

‫This means that the rendering process

play12:47

‫which is what the reconciler does, can be split into chunks,

play12:52

‫some tasks can be prioritized over others

play12:56

‫and work can be paused, reused,

play12:58

‫or thrown away if not valid anymore.

play13:02

‫Just keep in mind

play13:03

‫that all this happens automatically behind the scenes.

play13:07

‫It's completely invisible to us developers.

play13:10

‫There are, however, also some practical uses

play13:14

‫of this asynchronous rendering

play13:16

‫because it enables modern so-called concurrent features

play13:20

‫like Suspense or transitions starting in React 18.

play13:26

‫It also allows the rendering process

play13:28

‫to pause and resume later so that

play13:30

‫it won't block the browser's JavaScript engine

play13:33

‫with two long renders, which can be problematic

play13:37

‫for performance in large applications.

play13:40

‫And again, this is only possible

play13:43

‫because the render phase does not produce

play13:45

‫any visible output to the DOM yet.

play13:49

‫Okay, so at this point we know

play13:52

‫what the Fiber reconciler is and how the Fiber tree works

play13:56

‫but now it's time to talk about what Fiber actually does

play14:00

‫which is the reconciliation process.

play14:04

‫And the best way to explain how reconciliation works

play14:07

‫is by using a practical example.

play14:10

‫So let's take the virtual DOM

play14:12

‫and the corresponding Fiber tree from the last slide

play14:16

‫which corresponds to this piece of code right here.

play14:20

‫So in the app component, there is a piece

play14:23

‫of state called showModal, which is currently set to true

play14:27

‫and you can pause the video here to analyze it

play14:30

‫but it's not really necessary.

play14:33

‫So let's say now that the state is updated to false.

play14:38

‫This will then trigger a re-render

play14:40

‫which will create a new virtual DOM.

play14:43

‫And in this tree, the modal

play14:45

‫and all its children are actually gone

play14:48

‫because they are no longer displayed

play14:50

‫when showModal is not true.

play14:54

‫Also, all remaining React elements are yellow,

play14:58

‫meaning that all of them were re-rendered.

play15:01

‫And do you remember why that is?

play15:05

‫That's right.

play15:06

‫It's because all children of a re-rendered element

play15:10

‫are re-rendered as well,

play15:12

‫as we just learned a few minutes ago.

play15:15

‫But anyway, this new virtual DOM now needs

play15:18

‫to be reconciled with the current Fiber tree,

play15:22

‫which will then result in this updated tree

play15:25

‫which internally is called the work in progress tree.

play15:30

‫So whenever reconciliation needs to happen,

play15:33

‫Fiber walks through the entire tree step by step

play15:37

‫and analyzes exactly what needs to change

play15:40

‫between the current Fiber tree

play15:42

‫and the updated Fiber tree based on the new virtual DOM.

play15:48

‫And this process of comparing elements step-by-step

play15:51

‫based on their position in the tree is called diffing

play15:56

‫and we will explore exactly how diffing works

play15:59

‫a bit later in the section

play16:00

‫because that's actually pretty important in practice.

play16:05

‫But anyway, let's quickly analyze our updated Fiber tree

play16:09

‫where I marked new work that is related to DOM mutations.

play16:15

‫So first, the Btn component has some new text

play16:19

‫and so the work that will need to be done

play16:22

‫in this Fiber is a DOM update.

play16:25

‫So in this case, swapping text from height to rate.

play16:29

‫Then we have the Modal, Overlay, h3 and button.

play16:34

‫So these were in the current Fiber tree

play16:37

‫but are no longer in the virtual DOM

play16:40

‫and therefore they are marked as DOM deletions.

play16:44

‫Finally, we have the interesting case

play16:47

‫of the video component.

play16:49

‫So this component was re-rendered because it's a child

play16:53

‫of the app component, but it actually didn't change.

play16:57

‫And so as a result of reconciliation,

play17:00

‫the DOM will not be updated in this case.

play17:04

‫Now, once this process is over, all these DOM mutations

play17:08

‫will be placed into a list called the list of effects

play17:12

‫which will be used in the next phase,

play17:15

‫so in a commit phase, to actually mutate the DOM.

play17:19

‫Now, what I showed you here was actually still

play17:22

‫a bit oversimplified, if you can believe that,

play17:26

‫but I think that this is more than enough

play17:29

‫for you to understand how this process works.

play17:34

‫Okay, so that was quite a deep dive, but now we're back here

play17:40

‫in the high level overview of the render phase.

play17:44

‫So we learned that the results of the reconciliation process

play17:48

‫is a second updated Fiber tree, plus basically a list

play17:53

‫of DOM updates that need to be performed in the next phase.

play17:58

‫So React still hasn't written anything to the DOM yet

play18:02

‫but it has figured out this so-called list of effects.

play18:06

‫So this is the final result of the render phase

play18:10

‫as it includes the DOM operations that will finally be made

play18:14

‫in the commit phase, which is the topic of our next video.

play18:19

‫So if you don't want to break the flow,

play18:22

‫then let's move on there right now.

Rate This

5.0 / 5 (0 votes)

Related Tags
ReactWeb DevelopmentJavaScriptVirtual DOMReconciliationFiberFront-EndRenderingState ManagementTechnical Explanation