• 技术文章 >web前端 >js教程

    React中Diff算法是什么?Diff算法的策略及实现

    不言不言2018-09-28 17:27:32转载2394
    本篇文章给大家带来的内容是关于React中Diff算法是什么?Diff算法的策略及实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    1、什么是Diff算法

    2、React Diff算法解读

    388197929-5b86b10366837_articlex.png

    3650587496-5b8772c0e2719_articlex.png

    1、 如果为更新文本类型,内容不同就直接更新替换,并不会调用复杂的Diff算法:

     ReactDOMTextComponent.prototype.receiveComponent(nextText, transaction) {
        //与之前保存的字符串比较
        if (nextText !== this._currentElement) {
          this._currentElement = nextText;
          var nextStringText = '' + nextText;
          if (nextStringText !== this._stringText) {
            this._stringText = nextStringText;
            var commentNodes = this.getHostNode();
            // 替换文本元素
            DOMChildrenOperations.replaceDelimitedText(
              commentNodes[0],
              commentNodes[1],
              nextStringText
            );
          }
        }
      }

    2、对于自定义组件元素:

    class Tab extends Component {
        constructor(props) {
            super(props);
            this.state = {
                index: 1,
            }
        }
        shouldComponentUpdate() {
            ....
        }
        render() {
            return (
                <p>
                    <p>item1</p>
                    <p>item1</p>
                </p>
            )
        }
        
    }

    3、基本元素:

    ReactDOMComponent.prototype.receiveComponent = function(nextElement, transaction, context) {
        var prevElement = this._currentElement;
        this._currentElement = nextElement;
        this.updateComponent(transaction, prevElement, nextElement, context);
    }
    
    ReactDOMComponent.prototype.updateComponent = function(transaction, prevElement, nextElement, context) {
        //需要单独的更新属性
        this._updateDOMProperties(lastProps, nextProps, transaction, isCustomComponentTag);
        //再更新子节点
        this._updateDOMChildren(
          lastProps,
          nextProps,
          transaction,
          context
        );
    
        // ......
    }

    3、React中Diff算法的实现

    _updateChildren: function(nextNestedChildrenElements, transaction, context) {
        var prevChildren = this._renderedChildren;
        var removedNodes = {};
        var mountImages = [];
    
        // 获取新的子元素数组
        var nextChildren = this._reconcilerUpdateChildren(
          prevChildren,
          nextNestedChildrenElements,
          mountImages,
          removedNodes,
          transaction,
          context
        );
    
        if (!nextChildren && !prevChildren) {
          return;
        }
    
        var updates = null;
        var name;
        var nextIndex = 0;
        var lastIndex = 0;
        var nextMountIndex = 0;
        var lastPlacedNode = null;
    
        for (name in nextChildren) {
          if (!nextChildren.hasOwnProperty(name)) {
            continue;
          }
          var prevChild = prevChildren && prevChildren[name];
          var nextChild = nextChildren[name];
          if (prevChild === nextChild) {
            // 同一个引用,说明是使用的同一个component,所以我们需要做移动的操作
            // 移动已有的子节点
            // NOTICE:这里根据nextIndex, lastIndex决定是否移动
            updates = enqueue(
              updates,
              this.moveChild(prevChild, lastPlacedNode, nextIndex, lastIndex)
            );
    
            // 更新lastIndex
            lastIndex = Math.max(prevChild._mountIndex, lastIndex);
            // 更新component的.mountIndex属性
            prevChild._mountIndex = nextIndex;
    
          } else {
            if (prevChild) {
              // 更新lastIndex
              lastIndex = Math.max(prevChild._mountIndex, lastIndex);
            }
    
            // 添加新的子节点在指定的位置上
            updates = enqueue(
              updates,
              this._mountChildAtIndex(
                nextChild,
                mountImages[nextMountIndex],
                lastPlacedNode,
                nextIndex,
                transaction,
                context
              )
            );
    
    
            nextMountIndex++;
          }
    
          // 更新nextIndex
          nextIndex++;
          lastPlacedNode = ReactReconciler.getHostNode(nextChild);
        }
    
        // 移除掉不存在的旧子节点,和旧子节点和新子节点不同的旧子节点
        for (name in removedNodes) {
          if (removedNodes.hasOwnProperty(name)) {
            updates = enqueue(
              updates,
              this._unmountChild(prevChildren[name], removedNodes[name])
            );
          }
        }
      }

    4、基于中Diff的开发建议

    以上就是React中Diff算法是什么?Diff算法的策略及实现的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:segmentfault思否,如有侵犯,请联系admin@php.cn删除
    上一篇:带你彻底弄懂Javascript继承的原理! 下一篇:jquery与js的区别是什么?js与jquery的用法区别介绍
    Web大前端开发直播班

    相关文章推荐

    • diff算法使用详解• React怎么实现diff算法• diff算法使用技巧总结• 怎样实现React diff算法• 如何使用React diff算法• Diff算法的分析:利用React渲染流程分析
    1/1

    PHP中文网