Giter VIP home page Giter VIP logo

acquaintance-with-jsx-vdom's Introduction

Getting started with JSX

What is JSX

  • A good introduction to JSX can be found here What is JSX

What is JSX; tldr

  • JSX is just a format that is used, the real work is done by the transpiler
  • the transpiler takes this form and calls a function with the args
/** @jsx transform
*/
let vdom = <div id='foo'>Hello !</div>;

// this will be executed as
let vdom = transform(
  'div',
  { id: 'foo' },
  'Hello !'
);
  • we can define the function and its return values
  • here babel transpiles the JSX and returns a JSON object with a node-name, its attributes and children
/** @jsx transform
*/
function transform(nodeName, attributes, ...args) {
  let children = args.length? [].concat(args):{};
  return {nodeName, attributes, children};
}

let vdom = <div id='foo'>Hello !</div>;

JSON.stringify(vdom, null, 2);

{
  "nodeName": "div",
  "attributes": {
    "id": "foo"
  },
  "children": [
    "Hello !"
  ]
}

Getting started with Virtual DOM

What is a virtual DOM

  • Iโ€™m following the article here virtual dom part 1
  • virtual DOM is any kind of representation of the real DOM

Implementing virtual DOM

  • we make a virtual DOM tree representation
  • we diff this virtual DOM tree with our real DOM tree
  • change our real DOM where it is needed
  • we leverage the JSX as mentioned above
  • a createElement function which adds new elements eg: <div>
function createElement(vNode) {
  if(typeof vNode === 'string') {
    return document.createTextNode(vNode);
  }
  const parentElement = document.createElement(vNode.type);
  vNode.children
    .map(createElement)
    .forEach(parentElement.appendChild.bind(parentElement));
  return parentElement;
}
  • a changed function which compares 2 tree node elements
function changed(node1, node2) {
  return typeof node1 !== typeof node2 ||
         typeof node1 === 'string' && node1 !== node2 ||
         node1.type !== node2.type;
}
  • an updateElement function which updates our real DOM where necessary
function updateElement(rDom, newNode, oldNode, index = 0) {
  if (!oldNode) {
    rDom.appendChild(
      createElement(newNode)
    );
  } else if (!newNode) {
    rDom.removeChild(rDom.childNodes[index]);
  } else if (changed(newNode, oldNode)) {
    rDom.replaceChild(createElement(newNode), rDom.childNodes[index]);
  } else if (newNode.type) {
    const newLength = newNode.children.length;
    const oldLength = oldNode.children.length;
    for (let i = 0; i < newLength || i < oldLength; i++) {
      updateElement(rDom.childNodes[index], newNode.children[i], oldNode.children[i], i);
    }
  }
}

Virtual DOM advanced

  • a more practical virtual dom tutorial can be found here

acquaintance-with-jsx-vdom's People

Contributors

alenthomas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.