React virtual DOM explained in simple English/简单语言解释React的虚拟DOM


If you are using React or learning React, you must have heared of the term `Virtual DOM`. Now what is Virtual DOM and why does react use it?

如果你正在用或者学习React,你一定听过虚拟DOM这个词儿。那什么是虚拟DOM? React为啥要用它呢?

Real DOM

First things first, DOM stands for "Document Object Model". The DOM in simple words represents the UI of your application. Everytime there is a change in the state of your application UI, the DOM get updated to represent that change.

Now the catch is frequently manipulating the DOM affects performance, making it slow.


  首先,DOM是"Document Object Model"的缩写,即文档对象模型。简单来说,DOM代表着应用程序的UI。每次当应用程序UI的状态发生变化,DOM就会更新。


What makes DOM manipulation slow?

The DOM is represented as a tree data structure. Because of that, the updates and changes to the DOM are fast. But after that change, the updated element and it's children have to be re-rendered to update the application UI. The re-re-rendering or re-painting of the UI is what makes it slow. Therefore, the more UI you have, the more expensive the DOM updates could be, since they would need to be re-rendered for every DOM update.



Virtual DOM

That is where the concept of Virtual DOM comes in and performs significantly better than real DOM. The Virtual DOM is only a virtual presentation of the DOM. Everytime the state of our application changes, the Virtual DOM gets update instead of the real DOM. 

Well, you may ask `Isn't the virtual DOM doing the same thing as real DOM, this sounds double work? How can this be faster than just updating the real DOM?`

The answer is virtual DOM is much faster and efficient, here is why.





How is Virtual DOM faster

When new elements are added to UI, the virtual DOM which is represented as a tree is created. Each element is a node on this tree. If the state of any of these elements changes, a new virtual DOM tree is created. This tree is then compared or `differed` with the previous Virtual DOM tree.

Once this is done, the virtual DOM calculates the best possible method to make the changes to the real DOM. This ensures that there are minimal operations on the real DOM. Hence, reducing the performance cost of updating the real DOM.

The picture below shows the virtual DOM tree and diffing precess






The red circles represent the nodes that have changed. These nodes represent the UI elements that have had their state changed. The difference between the previous version of virtual DOM tree and current virtual DOM tree is calculated. The whole parent subtree is then gets re-rendered to give the updated UI. This updated tree is then batch updated to the real DOM.


How does React use Virtual DOM

Now that you have a fair understanding of what Virtual DOM is,  and how it can help with performance of you app, lets look into how React leverages the Virtual DOM.

In React, every piece of UI is a component, and each component has a state. React follows the Observable Pattern and listens for state change. When the state of a component changes, React updates the Virtual DOM tree. Once the VIrtual DOM has been updated, React then compares the current version with previous version Virtual DOM. This process is called `diffing`.

posted @ 2019-09-18 15:59  pkyou  阅读(...)  评论(...编辑  收藏