美文网首页
React的JSX与virtual dom与diff算法

React的JSX与virtual dom与diff算法

作者: 爱翔是我二媳妇 | 来源:发表于2021-01-13 12:02 被阅读0次

    JSX

    React 是怎么解析JSX语法的?
    要提出两个方法

    react.createElement

    这里有三个入参:type, config, children
    type: dom的类型,如div
    config: dom的属性,如style={{color: #fff}}

        // 给props赋值config
        for (propName in config) {
          if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
            props[propName] = config[propName];
          }
        }
    

    children:当前dom的子元素,可能有多个,也可能没有。
    源码中用var childrenLength = arguments.length - 2;来计算子元素的个数

      var childrenLength = arguments.length - 2; // 计算子元素的个数
    
      if (childrenLength === 1) {
        props.children = children;
      } else if (childrenLength > 1) {
        var childArray = Array(childrenLength);
    
        for (var i = 0; i < childrenLength; i++) {
          childArray[i] = arguments[i + 2];
        }
    
        {
          if (Object.freeze) {
            Object.freeze(childArray); // 冻结子元素
          }
        }
    
        props.children = childArray; // 将children赋值给props
      }
    

    打印出一个virtual dom

    image.png

    react.createElement源码

    function createElement(type, config, children) {
      var propName; // Reserved names are extracted
    
      var props = {};
      var key = null;
      var ref = null;
      var self = null;
      var source = null;
    
      if (config != null) {
        if (hasValidRef(config)) {
          ref = config.ref;
    
          {
            warnIfStringRefCannotBeAutoConverted(config);
          }
        }
    
        if (hasValidKey(config)) {
          key = '' + config.key;
        }
    
        self = config.__self === undefined ? null : config.__self;
        source = config.__source === undefined ? null : config.__source; // Remaining properties are added to a new props object
    
        for (propName in config) {
          if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
            props[propName] = config[propName];
          }
        }
      } // Children can be more than one argument, and those are transferred onto
      // the newly allocated props object.
    
    
      var childrenLength = arguments.length - 2;
    
      if (childrenLength === 1) {
        props.children = children;
      } else if (childrenLength > 1) {
        var childArray = Array(childrenLength);
    
        for (var i = 0; i < childrenLength; i++) {
          childArray[i] = arguments[i + 2];
        }
    
        {
          if (Object.freeze) {
            Object.freeze(childArray);
          }
        }
    
        props.children = childArray;
      } // Resolve default props
    
    
      if (type && type.defaultProps) {
        var defaultProps = type.defaultProps;
    
        for (propName in defaultProps) {
          if (props[propName] === undefined) {
            props[propName] = defaultProps[propName];
          }
        }
      }
    
      {
        if (key || ref) {
          var displayName = typeof type === 'function' ? type.displayName || type.name || 'Unknown' : type;
    
          if (key) {
            defineKeyPropWarningGetter(props, displayName);
          }
    
          if (ref) {
            defineRefPropWarningGetter(props, displayName);
          }
        }
      }
    
      return ReactElement(type, key, ref, self, source, ReactCurrentOwner.current, props);
    }
    

    reactDom.render()

    将JSX 翻译为 html。

    ReactDOM.render(
      <div>
        <input type="text" style={inputStyle} value={value}/> 
        <button>{buttonName}</button>
      </div>,
      document.getElementById("container")
    )
    

    diff算法

    tree

    逐层比较,不会移动,遇到不同只会删除和创建

    component

    组件差异性比较,不会移动,遇到不同只会删除和创建

    element (key)

    可以根据key移动/添加/删除,只会对老的节点做向后移的操作
    diff算法

    为什么不建议用index做key

    key的作用,如果key相同,则只会对比改变属性,反之,key不同,会卸载并重新加载。

    <ul>
      <li key = "0">松子</li>
      <li key = "1">开心果</li>
      <li key = "2">核桃</li>
    </ul>
    

    如果上述列表顺序发生变化,但若用index做key,实际index不会发生变化:

    <ul>
      <li key = "0">开心果</li>
      <li key = "1">核桃</li>
      <li key = "2">松子</li>
    </ul>
    

    如遇到非受控组件,则如果向数组头部添加元素,会导致value回显错误。

    <ul>
      {demo.map(((it, index)=>{
          return <input key={index}></input>
      }))}
    </ul>
    <Button onClick={()=>setDemo(["girl"].concat(demo))}>交换</Button>
    
    reactkey.gif

    相关文章

      网友评论

          本文标题:React的JSX与virtual dom与diff算法

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