1.0.5 • Published 6 years ago

hg-virtual-dom v1.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

hg-virtual-dom

An easy-to-use solution to reduce manual DOM manipulation.

中文文档

Usage

import { h, diff, patch, create } from "hg-virtual-dom";

// use `h` to create a virtual dom tree
var tree = h("div", { id: "container" }, [
  h("h1", { style: "color: blue" }, ["simple virtal dom"]),
  h("p", ["Hello, virtual-dom"]),
  h("ul", [h("li")])
]);

// use `create` generate a real dom from virtual dom.
var root = create(tree);

// generate another different virtual dom tree
var newTree = h("div", { id: "container" }, [
  h("h1", { style: "color: red" }, ["simple virtal dom"]),
  h("p", ["Hello, virtual-dom"]),
  h("ul", [h("li"), h("li")])
]);

// use `diff` diff two virtual dom trees and get patches
var patches = diff(tree, newTree);

// use `patch` apply patches to real dom
patch(root, patches);

You can checkout an another example in example folder.

You should always provide a unique key property for each child in array(just like ReactJS's keyed children) for hg-virtual-dom to reorder children instead of replacing the whole list when perform diff algorithm.

var root = h("ul", [
  h("li", { key: "1" }, ["Jerry"]),
  h("li", { key: "2" }, ["Tomy"]),
  h("li", { key: "3" }, ["Lucy"])
]);

var newRoot = h("ul", [
  h("li", { key: "1" }, ["Jerry"]),
  h("li", { key: "2" }, ["Tomy"]),
  h("li", { key: "4" }, ["Lily"]),
  h("li", { key: "3" }, ["Lucy"])
]);

// ensure `patches` is minimum
var patches = diff(root, newRoot);
1.0.5

6 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.2.0

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago