美文网首页
reconciliation: diff算法是调和的实现

reconciliation: diff算法是调和的实现

作者: Time_Notes | 来源:发表于2020-08-05 10:17 被阅读0次

    Reconciliation is the process through which React updates the DOM. When a component's state changes, React has to calculate if it is necessary to update the DOM. It does this by creating a virtual DOM and comparing it with the current DOM. In this context, the virtual DOM will contain the new state of the component.


    When your component is first initialized, the render method is called, generating a lightweight representation of your view. From that representation, a string of markup is produced and injected into the document. When your data changes, the render method is called again. In order to perform updates as efficiently as possible, we diff the return value from the previous call to render with the new one and generate a minimal set of changes to be applied to the DOM.

    The data returned from render is neither a string nor a DOM node — it’s a lightweight description of what the DOM should look like.

    We call this process reconciliation.

    React builds a representation of the browser Document Object Model or DOM in memory called the virtual DOM. As components are updated, React checks to see if the component’s HTML code in the virtual DOM matches the browser DOM. If a change is required, the browser DOM is updated. If nothing has changed, then no update is performed.

    As you know, this is called the reconciliation process and can be broken down into the following steps:

    Step 1: The virtual DOM is updated.

    Step 2: The virtual DOM is compared to the previous version of the virtual DOM and checks which elements have changed.

    Step 3: The changed elements are updated in the browser DOM.

    Step 4: The displayed webpage updates to match the browser DOM.

    As updating the browser DOM can be a slow operation, this process helps to reduce the number of updates to the browser DOM by only updating when it is necessary.


    Its job is to come up with the most optimized solution to resolve the difference between previous and current Virtual DOM state. And then apply the new Virtual DOM to the real DOM.


    调和:将Virtual DOM树转换成actual DOM树的最少操作的过程,React diff算法是调和的具体实现。

    React采用虚拟DOM技术实现对真实DOM的映射,即React Diff算法的差异查找实质是对两个JavaScript对象的差异查找

    首先需要明确,只有在React更新阶段才会有Diff算法的运用。

    相关文章

      网友评论

          本文标题:reconciliation: diff算法是调和的实现

          本文链接:https://www.haomeiwen.com/subject/ovuvkktx.html