8000 GitHub - Rain120/vdom-diff-algorithm: A simple algorithm implementation for vdom diff for react or vue dom
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rain120/vdom-diff-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

GitHub watchers STAR FORK

ISSUES GitHub closed pull requests COMMIT

LANGUAGES VERSION LICENSE

๐Ÿ˜š Welcome

Welcome to the Virtual Dom Diff Algorithm, The repo is a simple algorithm implementation for virtual dom diff for react or vue dom.

๐ŸŽฎ TL;DR

Demo

โœ Why am I doing this?

  • Learn something about virtual dom diff from react or vue dom calculate way.

๐Ÿ”จ How to understand it?

Through javaScript object description the Real DOM

We had used the babel plugin which named @babel/plugin-transform-react-jsx transform for this step.

Example

React

In

const profile = (
  <div>
    <img src="avatar.png" className="profile" />
    <h3>{[user.firstName, user.lastName].join(' ')}</h3>
  </div>
);

Out

const profile = React.createElement("div", null,
  React.createElement("img", { src: "avatar.png", className: "profile" }),
  React.createElement("h3", null, [user.firstName, user.lastName].join(" "))
);

More info go to @babel/plugin-transform-react-jsx

or you can create a class(ES6) to descript the dom, just like this:

class Element {
  type: string;
  props: Object<any>;
  children: Array<any>;

  constructor(type: stirng, props: Object<any>, children: Array<any>) {
    this.type = type;
    this.props = props;
    this.children = children;
  }
}

Render to real dom

The javascript virtual dom renders to the HTML real dom.

if (isString(node)) {
    return document.createTextNode(node + '');
  }
  const el = document.createElement(node.type);
  setProps(el, node.props || {});
  node.children &&
    node.children.map(createElement).forEach(el.appendChild.bind(el));

  return el;
function diff(oldNode, newNode) {
  if (!oldNode) {
    return { type: CREATE, newNode };
  }

  if (!newNode) {
    return { type: REMOVE };
  }

  if (changed(oldNode, newNode)) {
    return { type: REPLACE, newNode };
  }

  if (oldNode.type !== newNode.type) {
    return {
      type: UPDATE,
      props: diffProps(oldNode.props, newNode.props),
      children: diffChildren(oldNode.children, newNode.children)
    };
  }
}
function patch(parent, patches, index = 0) {
  if (!patches) {
    return;
  }
  const el = parent.children[index];

  switch (patches.type) {
    case CREATE: {
      const { newNode } = patches;
      const newEl = document.createElement(newNode);
      return parent.appendChild(newEl);
    }
    case REMOVE: {
      return parent.removeChild(el);
    }
    case REPLACE: {
      const { newNode } = patches;
      const newEl = createElement(newNode);
      return parent.replaceChild(newEl, el);
    }
    case UPDATE: {
      const { props, children } = patches;
      patchProps(el, props);
      children.forEach((child, idx) => {
        patch(el, child, idx);
      });
    }
  }
}

๐Ÿค Contributing

PR

We welcome all contributions. You can submit any ideas as pull requests or as a GitHub issue.

๐Ÿ”— Links

๐Ÿ‘จโ€๐Ÿญ Author

Front-End development engineer, technology stack: React + Typescript + Mobx, also used Vue + Vuex for a while

๐Ÿ“ License

MIT

Copyright ยฉ 2020-present Rain120.

โ˜• Coffee or Tea

wechat-zhifubao-pay.png

About

A simple algorithm implementation for vdom diff for react or vue dom

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0