What is Virtual DOM?

前端 未结 12 718
遥遥无期
遥遥无期 2020-11-28 01:39

Recently, I looked at Facebook\'s React framework. It uses a concept called \"the Virtual DOM,\" which I didn\'t really understand.

What is the Virtual DOM? What are

12条回答
  •  星月不相逢
    2020-11-28 02:22

    According to React doc: https://reactjs.org/docs/faq-internals.html#what-is-the-virtual-dom

    'In React world, the term “virtual DOM” is usually associated with React elements since they are the objects representing the user interface. '

    import React, { Component } from 'react'; //You need to do this inside a module to import
    
    class App extends Component{
       render(){
           return (
            //This returns a virtual DOM
           )
       }
    }
    
    

    The code inside return is actually a call to function React.createElement:

    //render can be rewritten like this:
    render(){
       return [
                React.createElement(
                    'button',
                    {
                        key: null,
                        ref: null,           
                    },
                    'Hi',
                )
       ]
    }
    

    which returns something like this:

    {
      $$typeof: Symbol.for('react.element'), 
      type: "button", 
      key: null, 
      ref: null, 
      props: { 
         children: 'Hi',
      }
    }
    

    and this is virtual DOM. It's an JavaScript object which costs much less to manipulate than the actual DOM element created by

    document.createElement('button');
    

    which is also a JavaScript object looks like this:

    accessKey: ""
    ariaAtomic: null
    ariaAutoComplete: null
    ariaBusy: null
    ariaChecked: null
    ariaColCount: null
    ariaColIndex: null
    ariaColSpan: null
    ariaCurrent: null
    ariaDescription: null
    ariaDisabled: null
    ariaExpanded: null
    ariaHasPopup: null
    ariaHidden: null
    ariaKeyShortcuts: null
    ariaLabel: null
    ariaLevel: null
    ariaLive: null
    ariaModal: null
    ariaMultiLine: null
    ariaMultiSelectable: null
    ariaOrientation: null
    ariaPlaceholder: null
    ariaPosInSet: null
    ariaPressed: null
    ariaReadOnly: null
    ariaRelevant: null
    ariaRequired: null
    ariaRoleDescription: null
    ariaRowCount: null
    ariaRowIndex: null
    ariaRowSpan: null
    ariaSelected: null
    ariaSetSize: null
    ariaSort: null
    ariaValueMax: null
    ariaValueMin: null
    ariaValueNow: null
    ariaValueText: null
    assignedSlot: null
    attributeStyleMap: StylePropertyMap {size: 0}
    attributes: NamedNodeMap {length: 0}
    autocapitalize: ""
    autofocus: false
    baseURI: "http://localhost:3000/"
    childElementCount: 0
    childNodes: NodeList []
    children: HTMLCollection []
    classList: DOMTokenList [value: ""]
    className: ""
    clientHeight: 0
    clientLeft: 0
    clientTop: 0
    clientWidth: 0
    contentEditable: "inherit"
    dataset: DOMStringMap {}
    dir: ""
    disabled: false
    draggable: false
    elementTiming: ""
    enterKeyHint: ""
    firstChild: null
    firstElementChild: null
    form: null
    formAction: "http://localhost:3000/"
    formEnctype: ""
    formMethod: ""
    formNoValidate: false
    formTarget: ""
    hidden: false
    id: ""
    innerHTML: ""
    innerText: ""
    inputMode: ""
    isConnected: false
    isContentEditable: false
    labels: NodeList []
    lang: ""
    lastChild: null
    lastElementChild: null
    localName: "button"
    name: ""
    namespaceURI: "http://www.w3.org/1999/xhtml"
    nextElementSibling: null
    nextSibling: null
    nodeName: "BUTTON"
    nodeType: 1
    nodeValue: null
    nonce: ""
    offsetHeight: 0
    offsetLeft: 0
    offsetParent: null
    offsetTop: 0
    offsetWidth: 0
    onabort: null
    onanimationend: null
    onanimationiteration: null
    onanimationstart: null
    onauxclick: null
    onbeforecopy: null
    onbeforecut: null
    onbeforepaste: null
    onbeforexrselect: null
    onblur: null
    oncancel: null
    oncanplay: null
    oncanplaythrough: null
    onchange: null
    onclick: null
    onclose: null
    oncontextmenu: null
    oncopy: null
    oncuechange: null
    oncut: null
    ondblclick: null
    ondrag: null
    ondragend: null
    ondragenter: null
    ondragleave: null
    ondragover: null
    ondragstart: null
    ondrop: null
    ondurationchange: null
    onemptied: null
    onended: null
    onerror: null
    onfocus: null
    onformdata: null
    onfullscreenchange: null
    onfullscreenerror: null
    ongotpointercapture: null
    oninput: null
    oninvalid: null
    onkeydown: null
    onkeypress: null
    onkeyup: null
    onload: null
    onloadeddata: null
    onloadedmetadata: null
    onloadstart: null
    onlostpointercapture: null
    onmousedown: null
    onmouseenter: null
    onmouseleave: null
    onmousemove: null
    onmouseout: null
    onmouseover: null
    onmouseup: null
    onmousewheel: null
    onpaste: null
    onpause: null
    onplay: null
    onplaying: null
    onpointercancel: null
    onpointerdown: null
    onpointerenter: null
    onpointerleave: null
    onpointermove: null
    onpointerout: null
    onpointerover: null
    onpointerrawupdate: null
    onpointerup: null
    onprogress: null
    onratechange: null
    onreset: null
    onresize: null
    onscroll: null
    onsearch: null
    onseeked: null
    onseeking: null
    onselect: null
    onselectionchange: null
    onselectstart: null
    onstalled: null
    onsubmit: null
    onsuspend: null
    ontimeupdate: null
    ontoggle: null
    ontransitionend: null
    onvolumechange: null
    onwaiting: null
    onwebkitanimationend: null
    onwebkitanimationiteration: null
    onwebkitanimationstart: null
    onwebkitfullscreenchange: null
    onwebkitfullscreenerror: null
    onwebkittransitionend: null
    onwheel: null
    outerHTML: ""
    outerText: ""
    ownerDocument: document
    parentElement: null
    parentNode: null
    part: DOMTokenList [value: ""]
    prefix: null
    previousElementSibling: null
    previousSibling: null
    scrollHeight: 0
    scrollLeft: 0
    scrollTop: 0
    scrollWidth: 0
    shadowRoot: null
    slot: ""
    spellcheck: true
    style: CSSStyleDeclaration {alignContent: "", alignItems: "", alignSelf: "", alignmentBaseline: "", all: "", …}
    tabIndex: 0
    tagName: "BUTTON"
    textContent: ""
    title: ""
    translate: true
    type: "submit"
    validationMessage: ""
    validity: ValidityState {valueMissing: false, typeMismatch: false, patternMismatch: false, tooLong: false, tooShort: false, …}
    value: ""
    willValidate: true
    

    You can learn more about virtual DOM and React at https://indepth.dev/inside-fiber-in-depth-overview-of-the-new-reconciliation-algorithm-in-react/

提交回复
热议问题