Socket
Book a DemoInstallSign in
Socket

@alexcambose/virtual-dom

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alexcambose/virtual-dom

A Virtual DOM algorithm implementation that improves front end performance by updating only changed nodes in the DOM.

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
16
45.45%
Maintainers
1
Weekly downloads
 
Created
Source

virtual-dom

Build Status

A Virtual DOM algorithm implementation that improves front end performance by updating only changed nodes in the DOM.

DEMO

Installation

As npm package

npm i -S @alexcambose/virtual-dom

Standalone script file

<script src="dist/virtual-dom.js"></script>

Usage

let newContent = null;

// function thet will create virtual-dom object
const createDom = title => (
    h('div', null,
        h('h1', { className: 'header' }, title),
        h('p', null , 'Lorem ipsum'),
        h('footer', { className: 'footer' }, 'The footer'),
    )
);

// create virtual-dom object
let content = createDom('Some title');

// render the virtual dom object into the real dom
render(content, document.getElementById('app'));

const rerender = () => {

    // create the virtual-dom object again but with dfferent properties
    newContent = createDom('Some other title');

    // patches object that will be used to modify changed elements
    const patches = diff(content, newContent);

    //appy patches
    patch(appContainer, patches);

    // update content
    content = newContent;
};

API

  • h(tagName, props, ...children) - Function to define virtual-dom using hyperscript style
h('div', { className: 'section' },
    h('h1', { style: {color: 'red'} }, 'Section title'),
));

will output

{
   type: "div",
   props: {
      className: "section",
      children: [
         {
            type: "h1",
            props: {
               style: {
                  color: "red"
               },
               children: [
                  "Section title"
               ]
            }
         }
      ]
   }
}
  • render(VDOM, domElement) - renders the virtual dom object into a specified element in dom
// content = h(..., h(...))
render(content, document.getElementById('app'));
  • diff(oldVDOM, newVDOM) - create a patches object that contains all the differences between two virtual-dom objects, should be used with patch
const oldVDOM = h('h1', null, 'Hello title');
const newVDOM = h('h1', { className: 'header' }, 'Hello new class');
const patches = diff(oldVDOM, newVDOM);

patches will look something like this

{
   0:{
      childrenPatches: {
         0: {
            selfPatch: {
               type: PATCH_TEXT_NODE,
               payload: "Hello new class"
            }
         }
      }
   }
}
  • patch(domElement, patches) - updates the dom based on the patches produced by diff
const patches = diff(oldVDOM, newVDOM);
patch(document.getElementById('app'), patches);

License

MIT

Keywords

virtual-dom

FAQs

Package last updated on 13 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.