Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What the v...DOM?

What the v...DOM?

stefan judis

April 18, 2018
Tweet

More Decks by stefan judis

Other Decks in Technology

Transcript

  1. Go out of the door and turn left.
 Get on

    Frankfurter Allee until you get to the Proskauer Straße.
 Go left at the park. Go to the end of the park.
 My house is #22.
  2. “ The declarative programming paradigm expresses the logic of a

    computation without describing its control flow.
  3. Imperative function double (arr) { let results = []; for

    (let i = 0; i < arr.length; i++){ results.push(arr[i] * 2); } return results; } Declarative function double (arr) { return arr.map((item) => item * 2); }
  4. const $btn = document.getElementById('btn'); $btn.addEventListener('click', _ => { $btn.classList.toggle('is-highlighted'); $btn.innerText

    = $btn.innerText === 'Highlight me!' ? 'Please stop!' : 'Highlight me!'; }); Imperative UI
  5. export default class App extends Component { ... render(props, {

    highlighted }) { return ( <button type="button" highlighted={highlighted} onclick={this.toggleHighlight}> { highlighted ? 'Please stop!': 'Highlight me!' } </button> ); } } codesandbox.io/s/rwjol2n3op
  6. export default class App extends Component { ... render(props, {

    highlighted }) { return ( <button type="button" highlighted={highlighted} onclick={this.toggleHighlight}> { highlighted ? 'Please stop!': 'Highlight me!' } </button> ); } } codesandbox.io/s/rwjol2n3op THAT'S NOT JAVASCRIPT!?
  7. const {parse} = require('babylon'); const code = ` const year

    = (new Date()).getFullYear(); const msg = ':tada: Frontend Con ${year}! :tada:'; console.log(msg); `; const ast = parse(code); ./my-own-code-transform/index.js
  8. “In computer science, an abstract syntax tree (AST), [...], is

    a tree representation of the abstract syntactic structure of source code written in a programming language.
  9. { "type": "Program", "start": 0, "end": 105, "loc": { "start":

    { "line": 1, "column": 0 }, "end": { "line": 3, "column": 17 } } }
  10. { "type": "VariableDeclaration", "start": 0, "end": 40, "loc": { "start":

    { "line": 1, "column": 0 }, "end": { "line": 1, "column": 40 } }, "declarations": [...], "kind": "const" }
  11. { "type": "ExpressionStatement", "start": 88, "end": 105, "loc": { "start":

    { "line": 3, "column": 0 }, "end": { "line": 3, "column": 17 } }, "expression": {...} }
  12. ./example/input.js VariableDeclarator const msg = `:tada: Frontend Con ${year}! :tada:`;

    VariableDeclaration Identifier TemplateLiteral TemplateElement
  13. ./example/input.js VariableDeclarator const msg = `:tada: Frontend Con ${year}! :tada:`;

    VariableDeclaration Identifier TemplateLiteral TemplateElement Identifier
  14. ./example/input.js VariableDeclarator const msg = `:tada: Frontend Con ${year}! :tada:`;

    VariableDeclaration Identifier TemplateLiteral TemplateElement TemplateElement Identifier
  15. const {parse} = require('babylon'); const code = `...`; const ast

    = parse(code); ./my-own-code-transform/index.js
  16. const {parse} = require('babylon'); const {default: traverse} = require('@babel/traverse'); const

    code = `...`; const ast = parse(code); traverse(ast, { enter ({node}) { if (node.type === 'TemplateElement') { node.value.raw = node.value.raw.replace(/:tada:/g, '\u{1F389}'); } } }); ./my-own-code-transform/index.js
  17. const {parse} = require('babylon'); const {default: traverse} = require('@babel/traverse'); const

    code = `...`; const ast = parse(code); traverse(ast, { enter ({node}) { if (node.type === 'TemplateElement') { node.value.raw = node.value.raw.replace(/:tada:/g, '\u{1F389}'); } } }); ./my-own-code-transform/index.js
  18. const {parse} = require('babylon'); const {default: traverse} = require('@babel/traverse'); const

    code = `...`; const ast = parse(code); traverse(ast, { enter (path) { /* ... */ } }); ./my-own-code-transform/index.js
  19. ./my-own-code-transform/index.js const {parse} = require('babylon'); const {default: traverse} = require('@babel/traverse');

    const {default: generate} = require('@babel/generator'); const code = `...`; const ast = parse(code); traverse(ast, { enter (path) { /* ... */ } }); console.log(generate(ast).code);
  20. ./lib/index.js export default function () { return { visitor: {

    TemplateElement({node}) { node.value.raw = node.value.raw.replace( /:tada:/g, '\u{1F389}' ); } } }; }
  21. export default function () { return { visitor: { TemplateElement({node})

    { node.value.raw = node.value.raw.replace( /:tada:/g, '\u{1F389}' ); } } }; } ./lib/index.js
  22. export default function () { return { visitor: { TemplateElement({node})

    { node.value.raw = node.value.raw.replace( /:tada:/g, '\u{1F389}' ); } } }; } ./lib/index.js
  23. export default function () { return { visitor: { TemplateElement({node})

    { node.value.raw = node.value.raw.replace( /:tada:/g, '\u{1F389}' ); } } }; } ./lib/index.js
  24. const App = () => { return <h1>:tada: Frontend Con

    2018! :tada:</h1>; }; babel-plugin-transform-react-jsx ReturnStatement
  25. const App = () => { return <h1>:tada: Frontend Con

    2018! :tada:</h1>; }; babel-plugin-transform-react-jsx ReturnStatement JSXElement
  26. const App = () => { return <h1>:tada: Frontend Con

    2018! :tada:</h1>; }; babel-plugin-transform-react-jsx ReturnStatement JSXElement JSXOpeningElement
  27. const App = () => { return <h1>:tada: Frontend Con

    2018! :tada:</h1>; }; babel-plugin-transform-react-jsx ReturnStatement JSXElement JSXOpeningElement JSXText
  28. const App = () => { return <h1>:tada: Frontend Con

    2018! :tada:</h1>; }; babel-plugin-transform-react-jsx ReturnStatement JSXElement JSXOpeningElement JSXText JSXClosingElement
  29. const App = () => { return React.createElement( "h1", null,

    "! Frontend Con 2018! !" ); }; const App = () => { return <h1>:tada: Frontend Con 2018! :tada:</h1>; };
  30. const App = () => { return <h1>:tada: Frontend Con

    2018! :tada:</h1>; }; const App = () => { return React.createElement( "h1", null, "! Frontend Con 2018! !" ); };
  31. const App = () => { return <h1>:tada: Frontend Con

    2018! :tada:</h1>; }; const App = () => { return React.createElement( "h1", null, "! Frontend Con 2018! !" ); }; REACT?
  32. /* @jsx heroify */ const App = () => {

    return <h1>:tada: Frontend Con 2018! :tada:</h1>; }; babel-plugin-transform-react-jsx
  33. /* @jsx heroify */ const App = () => {

    return heroify( "h1", null, "! Frontend Con 2018! !" ); }; /* @jsx heroify */ const App = () => { return <h1>:tada: Frontend Con 2018! :tada:</h1>; };
  34. /* @jsx h */ const App = () => {

    return <h1>:tada: Frontend Con 2018! :tada:</h1>; }; /* @jsx h */ const App = () => { return h( "h1", null, "! Frontend Con 2018! !" ); };
  35. /* @jsx h */ import {h} from 'preact'; const App

    = () => { return <h1>:tada: Frontend Con 2018! :tada:</h1>; }; /* @jsx h */ import {h} from 'preact'; const App = () => { return h( "h1", null, "! Frontend Con 2018! !" ); };
  36. /* @jsx h */ import {h} from 'preact'; const App

    = () => { return <h1>:tada: Frontend Con 2018! :tada:</h1>; }; /* @jsx h */ import {h} from 'preact'; const App = () => { return h( "h1", null, "! Frontend Con 2018! !" ); }; JSX is only one option
  37. <!DOCTYPE html> <html> <head> <title>You don't need JSX</title> <link rel="stylesheet"

    href="https: //contentful.github.io/ .../cf-extension.css"> <script src="https: //unpkg.com/contentful-ui-extensions-sdk@3"> </script> </head> <body> </body> </html>
  38. <!DOCTYPE html> <html> <head> <title>You don't need JSX</title> <link rel="stylesheet"

    href="https: //contentful.github.io/ .../cf-extension.css"> <script src="https: //unpkg.com/contentful-ui-extensions-sdk@3"> </script> <script> /* Preact 8.2.9 */ !function () { "use strict"; function e() { } function ... </script> </head> <body> </body> </html>
  39. <!DOCTYPE html> <html> <head> <title>You don't need JSX</title> <link rel="stylesheet"

    href="https: //contentful.github.io/ .../cf-extension.css"> <script src="https: //unpkg.com/contentful-ui-extensions-sdk@3"> </script> <script> /* Preact 8.2.9 */ !function () { "use strict"; function e() { } function ... </script> </head> <body> <script> const { h, render } = window.preact; </script> </body> </html>
  40. <!DOCTYPE html> <html> <head> <title>You don't need JSX</title> <link rel="stylesheet"

    href="https: //contentful.github.io/ .../cf-extension.css"> <script src="https: //unpkg.com/contentful-ui-extensions-sdk@3"> </script> <script> /* Preact 8.2.9 */ !function () { "use strict"; function e() { } function ... </script> </head> <body> <script> const { h, render } = window.preact; render( h('h1', {class: 'headline-1'}, '! Frontend Con 2018! !'), document.body ); </script> </body> </html>
  41. const h = (nodeName, attributes, ...children) => { const el

    = document.createElement(nodeName); return el; }; ./our-hyperscript.js
  42. const h = (nodeName, attributes, ...children) => { const el

    = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } return el; }; ./our-hyperscript.js
  43. const h = (nodeName, attributes, ...children) => { const el

    = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } return el; }; ./our-hyperscript.js
  44. const h = (nodeName, attributes, ...children) => { const el

    = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } children.forEach(child => { if (typeof child === 'string') { el.appendChild(document.createTextNode(child)); } else { el.appendChild(child); } }); return el; }; ./our-hyperscript.js
  45. import h from './our-hyperscript.js'; const App = () => {

    return h( "h1", null, "! Frontend Con 2018! !" ); }; console.log('App()', App()); ./app.js
  46. import h from './our-hyperscript.js'; const App = () => {

    return h( "h1", null, "! Frontend Con 2018! !" ); }; console.log('App()', App()); ./app.js
  47. const App = () => { return h( "h1", null,

    "! Frontend Con 2018! !" ); }; ./app.js
  48. const App = (props) => { const {list} = props;

    return h('div', {class: 'app'}, h('h1', null, '! Frontend Con 2018! !'), h('ul', null, ...list.map(item => h('li', null, item)) ) ); }; ./app.js
  49. ./app.js const App = (props) => { const {list} =

    props; return ( <div class="app">, <h1>! Frontend Con 2018! !</h1> <ul> {...list.map(item => <li>{item}</li>)} </ul> </div> ); };
  50. const App = (props) => { const {list} = props;

    return h('div', {class: 'app'}, h('h1', null, '! Frontend Con 2018! !'), h('ul', null, ...list.map(item => h('li', null, item)) ); ); }; ./app.js
  51. let currentApp; const App = (props) => {...}; const render

    = (state) => { const newApp = App(state); if (currentApp) { document.body.replaceChild(newApp, currentApp); } else { document.body.appendChild(newApp); } currentApp = newApp; }; ./app.js
  52. let currentApp; const App = (props) => {...}; const render

    = (state) => {...}; const state = { list: [ '"', '#', '$', '%', '&', ''', '(', ')', '*', '+' ] }; ./app.js
  53. let currentApp; const App = (props) => {...}; const render

    = (state) => {...}; const state = { list: [ '"', '#', '$', '%', '&', ''', '(', ')', '*', '+' ] }; render(state); ./app.js
  54. let currentApp; const App = (props) => {...}; const render

    = (state) => {...}; const state = {...}; render(state); ./app.js
  55. let currentApp; const App = (props) => {...}; const render

    = (state) => {...}; const state = {...}; render(state); setInterval(_ => { state.list = [ ...state.list, getRandomItemFromArray(state.list) ]; render(state); }, 1000); ./app.js
  56. “We want to render components and have them updated only

    when data changes - that's where the power of vDOM diffing shines.
  57. Features Community Performance Which one should I use? DX Documentation

    Ecosystem Stability There are always trade-offs...
  58. h()

  59. const h = (nodeName, attributes, ...children) => { const el

    = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } children.forEach(child => { if (typeof child === 'string') { el.appendChild(document.createTextNode(child)); } else { el.appendChild(child); } }) return el; } ./our-vdom.js
  60. ./our-vdom.js const h = (nodeName, attributes, ...children) => { return

    {nodeName, attributes, children}; } h(...) → virtual DOM nodes
  61. const App = (props) => { const {list} = props;

    return h('div', {class: 'app'}, h('h1', null, '! Frontend Con 2018! !'), h('ul', null, ...list.map(item => h('li', null, item)) ) ); }; ./app.js
  62. const App = (props) => {...}; ./app.js { "nodeName": "div",

    "attributes": { "class": "app" }, "children": [ { "nodeName": "h1", "attributes": null, "children": ["! Frontend Con 2018! !"] }, { "nodeName": "ul", "attributes": null, "children": [ { "nodeName": "li", "attributes": null, "children": ["""] },
  63. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; } ./our-vdom.js
  64. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); } ./our-vdom.js
  65. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); el = document.createElement(nodeName); } ./our-vdom.js
  66. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); el = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } } ./our-vdom.js
  67. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); el = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } (children || []).forEach(c => el.appendChild(renderNode(c))); return el; } ./our-vdom.js
  68. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); el = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } (children || []).forEach(c => el.appendChild(renderNode(c))); return el; } ./our-vdom.js
  69. let currentApp; const App = (props) => {...}; const render

    = (state) => { const newApp = App(state); if (currentApp) { document.body.replaceChild(newApp, currentApp); } else { document.body.appendChild(newApp); } currentApp = newApp; }; ./app.js
  70. let currentApp; const App = (props) => {...}; const render

    = (state) => { const newApp = renderNode(App(state)); if (currentApp) { document.body.replaceChild(newApp, currentApp); } else { document.body.appendChild(newApp); } currentApp = newApp; }; ./app.js
  71. ./app.js const App = ({list}) => { return h('div', {class:

    'app'}, h('h1', null, '! Frontend Con 2018! !'), h('ul', null, ...list.map(item => h('li', null, item)) ); ); };
  72. ./app.js class App { constructor () { this.state = {

    list: ['"', '#', '$', ...] }; } render (props, state) { const {list} = state; return h('div', {class: 'app'}, h('h1', null, '! Frontend Con 2018! !'), h('ul', null, ...list.map(item => h('li', null, item)) ) ); } }
  73. ./app.js class App { constructor () { this.state = {

    list: ['"', '#', '$', ...] }; } render (props, {list}) { return h('div', {class: 'app'}, h('h1', null, '! Frontend Con 2018! !'), h('ul', null, ...list.map(item => h('li', null, item)) ) ); } }
  74. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); el = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } (children || []).forEach(c => el.appendChild(renderNode(c))); return el; } ./our-vdom.js
  75. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); if (typeof nodeName === 'string') { el = document.createElement(nodeName); for (let key in attributes) { el.setAttribute(key, attributes[key]); } (children || []).forEach(c => el.appendChild(renderNode(c))); } return el; } ./our-vdom.js
  76. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); if (typeof nodeName === 'string') { // ... } return el; } ./our-vdom.js
  77. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); if (typeof nodeName === 'string') { // ... } else if (typeof nodeName === 'function') { // ... } return el; } ./our-vdom.js
  78. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); if (typeof nodeName === 'string') { // ... } else if (typeof nodeName === 'function') { // ... } return el; } ./our-vdom.js overreacted.io/how-does-react-tell-a-class-from-a-function/
  79. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); if (typeof nodeName === 'string') { // ... } else if (typeof nodeName === 'function') { // ... } return el; } ./our-vdom.js
  80. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); if (typeof nodeName === 'string') { // ... } else if (typeof nodeName === 'function') { const component = new nodeName(attributes); el = renderNode(component.render(component.props, component.state)); component.base = el; } return el; } ./our-vdom.js
  81. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); if (typeof nodeName === 'string') { // ... } else if (typeof nodeName === 'function') { const component = new nodeName(attributes); el = renderNode(component.render(component.props, component.state)); component.base = el; } return el; } ./our-vdom.js
  82. const renderNode = (vnode) => { let el; const {nodeName,

    attributes, children} = vnode; if (vnode.split) return document.createTextNode(vnode); if (typeof nodeName === 'string') { // ... } else if (typeof nodeName === 'function') { const component = new nodeName(attributes); el = renderNode(component.render(component.props, component.state)); component.base = el; } return el; } ./our-vdom.js A COMPONENT HOLDS A DOM REFERENCE
  83. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; } render (props, {list}) { return h('div', {class: 'app'}, h('h1', null, '! Frontend Con 2018! !'), h('ul', null, ...list.map(item => h('li', null, item)) ) ); } }
  84. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; } render (props, {list}) {...} }
  85. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; this.timer = setInterval(_ => { this.setState({ list: [...this.state.list, getRandomItemFromArray(state.list)] }); }, 1000); } render (props, {list}) {...} }
  86. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; this.timer = setInterval(_ => { this.setState({ list: [...this.state.list, getRandomItemFromArray(state.list)] }); }, 1000); } setState(state) { this.state = Object.assign(this.state, state); renderComponent(this); } render (props, {list}) {...} }
  87. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; this.timer = setInterval(_ => { this.setState({ list: [...this.state.list, getRandomItemFromArray(state.list)] }); }, 1000); } setState(state) { this.state = Object.assign(this.state, state); renderComponent(this); } render (props, {list}) {...} }
  88. const renderComponent = (component) => { const oldBase = component.base;

    component.base = renderNode( component.render(component.props, component.state) ); }; ./our-vdom.js
  89. const renderComponent = (component) => { const oldBase = component.base;

    component.base = renderNode( component.render(component.props, component.state) ); oldBase.parentNode.replaceChild(component.base, oldBase); }; ./our-vdom.js
  90. const renderComponent = (component) => { const oldBase = component.base;

    component.base = renderNode( component.render(component.props, component.state) ); oldBase.parentNode.replaceChild(component.base, oldBase); } COMPONENTS 
 RENDER 
 AUTONOMOUSLY ./our-vdom.js
  91. const renderComponent = (component) => { const oldBase = component.base;

    component.base = renderNode( component.render(component.props, component.state) ); oldBase.parentNode.replaceChild(component.base, oldBase); }; ./our-vdom.js
  92. const renderComponent = (component) => { const oldBase = component.base;

    component.base = renderNode( component.render(component.props, component.state) ); oldBase.parentNode.replaceChild(component.base, oldBase); }; DOM ./our-vdom.js
  93. const renderComponent = (component) => { const oldBase = component.base;

    component.base = renderNode( component.render(component.props, component.state) ); oldBase.parentNode.replaceChild(component.base, oldBase); }; virtual DOM ./our-vdom.js
  94. { nodeName: 'div', attributes: { key: 'value' }, children: [

    'text node' ] } Element( nodeName: 'div', attributes: { getItem('key'), setItem('key', 'value') }, childNodes: [ TextNode('text node') ] ) DOM virtual DOM ./our-vdom.js
  95. const renderComponent = (component) => { const oldBase = component.base;

    component.base = renderNode( component.render(component.props, component.state) ); oldBase.parentNode.replaceChild(component.base, oldBase); }; ./our-vdom.js
  96. const renderComponent = (component) => { const rendered = component.render(

    component.props, component.state ); }; ./our-vdom.js
  97. const renderComponent = (component) => { const rendered = component.render(

    component.props, component.state ); diff(component.base, rendered); }; ./our-vdom.js
  98. const renderComponent = (component) => { const rendered = component.render(

    component.props, component.state ); component.base = diff(component.base, rendered); }; ./our-vdom.js
  99. const diff = (dom, vnode) => { const newDom =

    renderNode(vnode); dom.parentNode.replaceChild(newDom, dom); return newDom; }; ./our-vdom.js
  100. const diff = (dom, vnode) => { const hasNewKids =

    dom.childNodes.length !== vnode.children.length; if (hasNewKids) { } else { const newDom = renderNode(vnode); dom.parentNode.replaceChild(newDom, dom); return newDom; } }; ./our-vdom.js
  101. const diff = (dom, vnode) => { const hasNewKids =

    dom.childNodes.length !== vnode.children.length; if (hasNewKids) { dom.appendChild( renderNode(vnode.children[vnode.children.length - 1]) ); return dom; } else { const newDom = renderNode(vnode); dom.parentNode.replaceChild(newDom, dom); return newDom; } }; ./our-vdom.js
  102. DOM { nodeName: 'div', attributes: { key: 'value' }, children:

    [ 'text node' ] } Element( nodeName: 'div', attributes: { getItem('key'), setItem('key', 'value') }, childNodes: [ TextNode('text node') ] ) virtual DOM
  103. DOM { nodeName: 'div', attributes: { key: 'value' }, children:

    [ 'text node' ] } Element( nodeName: 'div', attributes: { getItem('key'), setItem('key', 'value') }, childNodes: [ TextNode('text node') ] ) virtual DOM
  104. DOM { nodeName: 'div', attributes: { key: 'value' }, children:

    [ 'text node' ] } Element( nodeName: 'div', attributes: { getItem('key'), setItem('key', 'value') }, childNodes: [ TextNode('text node') ] ) virtual DOM
  105. DOM { nodeName: 'div', attributes: { key: 'value' }, children:

    [ 'text node' ] } Element( nodeName: 'div', attributes: { getItem('key'), setItem('key', 'value') }, childNodes: [ TextNode('text node') ] ) virtual DOM
  106. ./app.js const diff = (dom, vnode) => { const hasNewKids

    = dom.childNodes.length !== vnode.children.length; if (hasNewKids) { dom.appendChild( renderNode(vnode.children[vnode.children.length - 1]) ); return dom; } else { const newDom = renderNode(vnode); dom.parentNode.replaceChild(newDom, dom); return newDom; } };
  107. ./app.js const diff = (dom, vnode) => { // compare

    type // compare attributes // compare children };
  108. ./app.js const diff = (dom, vnode) => { // compare

    type // compare attributes // compare children // optimise as much as you can! };
  109. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; this.timer = setInterval(_ => { this.setState({ list: [...this.state.list, getRandomItemFromArray(state.list)] }); }, 1000); } setState(state) { this.state = Object.assign(this.state, state); renderComponent(this); } render (props, {list}) {...} }
  110. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; this.timer = setInterval(_ => { this.setState({ list: [...this.state.list, getRandomItemFromArray(state.list)] }); }, 1000); } setState(state) { this.prevState = Object.assign({}, this.state); this.state = Object.assign(this.state, state); renderComponent(this); } render (props, {list}) {...} }
  111. const renderComponent = (component) => { const rendered = component.render(

    component.props, component.state ); component.base = diff(component.base, rendered); }; ./our-vdom.js
  112. const renderComponent = (component) => { const {state, props} =

    component; component.state = component.prevState; component.props = component.prevProps; const rendered = component.render( component.props, component.state ); component.base = diff(component.base, rendered); }; ./our-vdom.js
  113. const renderComponent = (component) => { const {state, props} =

    component; component.state = component.prevState; component.props = component.prevProps; const rendered = component.render( component.props, component.state ); component.base = diff(component.base, rendered); }; ./our-vdom.js
  114. const renderComponent = (component) => { const {state, props} =

    component; component.state = component.prevState; component.props = component.prevProps; const rendered = component.render( component.props, component.state ); component.base = diff(component.base, rendered); }; ./our-vdom.js
  115. const renderComponent = (component) => { const {state, props} =

    component; component.state = component.prevState; component.props = component.prevProps; const rendered = component.render( component.props, component.state ); component.base = diff(component.base, rendered); }; ./our-vdom.js
  116. const renderComponent = (component) => { const {state, props} =

    component; component.state = component.prevState; component.props = component.prevProps; if (component.shouldComponentUpdate(props, state)) { component.props = props; component.state = state; const rendered = component.render( component.props, component.state ); component.base = diff(component.base, rendered); } }; ./our-vdom.js
  117. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; this.timer = setInterval(_ => { this.setState({ list: [...this.state.list, getRandomItemFromArray(state.list)] }); }, 1000); } setState(state) { this.prevState = Object.assign({}, state); this.state = Object.assign(this.state, state); renderComponent(this); } render (props, {list}) {...} }
  118. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; this.timer = setInterval(_ => { this.setState({ list: [...this.state.list, getRandomItemFromArray(state.list)] }); }, 1000); } setState(state) {...} render (props, {list}) {...} }
  119. ./app.js class App { constructor () { this.state = {

    list: [ '"', '#', '$', ... ] }; this.timer = setInterval(_ => { this.setState({ list: [...this.state.list, getRandomItemFromArray(state.list)] }); }, 1000); } shouldComponentUpdate(nextProps, nextState) { return this.state.list.length !== nextState.list.length; } setState(state) {...} render (props, {list}) {...} }
  120. parcelRequire=function(e,r,n){var t="function"==typeof parcelRequire&&parcelRequire,i="function"==typeof require&&require;function u(n,o){if(!r[n]){if(!e[n]) {var f="function"==typeof parcelRequire&&parcelRequire;if(!o&&f)return f(n,!0);if(t)return t(n,!0);if(i&&"string"==typeof

    n)return i(n);var c=new Error("Cannot find module '"+n+"'");throw c.code="MODULE_NOT_FOUND",c}a.resolve=function(r){return e[n][1][r]||r};var l=r[n]=new u.Module(n);e[n] [0].call(l.exports,a,l,l.exports)}return r[n].exports;function a(e){return u(a.resolve(e))}}u.isParcelRequire=!0,u.Module=function(e) {this.id=e,this.bundle=u,this.exports={}},u.modules=e,u.cache=r,u.parent=t;for(var o=0;o<n.length;o++)u(n[o]);return u}({4:[function(require,module,exports) { var e=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=! 0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();function t(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}function n(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function r(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:! 1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}function o(e,t){if(!(e instanceof t))throw new T y p e E r r o r ( " C a n n o t c a l l a c l a s s a s a f u n c t i o n " ) } v a r i = f u n c t i o n ( e , t ) { f o r ( v a r n = a r g u m e n t s . l e n g t h , r = A r r a y ( n > 2 ? n - 2 : 0 ) , o = 2 ; o < n ; o + +)r[o-2]=arguments[o];return{nodeName:e,attributes:t,children:r}},a=function(e){var t=e.base,n=e.render(e.props,e.state),r=u(t,n);e.base=r},u=function e(t,n,r) {if(t){if("string"==typeof n)return t.nodeValue=n,t;if("function"==typeof n.nodeName){var o=new n.nodeName(n.attributes);return e(t,o.render(o.props,o.state)),t}t.childNodes.length!==n.children.length&&t.appendChild(f(n.children[n.children.length-1]));for(var i=0;i<n.children.length;i+ +)e(t.childNodes[i],n.children[i]);return t}var a=f(n);return r.appendChild(a),a},s=function(){function t(e){o(this,t),this.props=e,this.state={}}return e(t, [{key:"setState",value:function(e){this.state=Object.assign(this.state,e),a(this)}}]),t}(),l=function(a){function u(e){o(this,u);var r=n(this,(u.__proto__|| Object.getPrototypeOf(u)).call(this,e));return r.state.list=["" ","# ","$ ","% ","& ","' ","( ",") ","* ","+ "],r.timer=setInterval(function() {r.setState({list:[].concat(t(r.state.list),[r.state.list[Math.floor(Math.random()*r.state.list.length)]])})},1e3),r}return r(u,s),e(u, [{key:"render",value:function(e,t){var n=t.list;return i("div",{class:"app"},i("h1",null,"! JSHeroes 2018! ! "),i(c,{list:n}))}}]),u}(),c=function(a){function u(){return o(this,u),n(this,(u.__proto__||Object.getPrototypeOf(u)).apply(this,arguments))}return r(u,s),e(u,[{key:"render",value:function(e,n){return i . a p p l y ( v o i d 0 , [ " u l " , n u l l ] . c o n c a t ( t ( e . l i s t . m a p ( f u n c t i o n ( e ) { r e t u r n i ( " l i " , n u l l , e ) } ) ) ) ) } } ] ) , u } ( ) , f = f u n c t i o n e ( t ) { v a r n = v o i d 0 , r = t . n o d e N a m e , o = t . a t t r i b u t e s , i = t . c h i l d r e n ; i f ( t . s p l i t ) r e t u r n d o c u m e n t . c r e a t e T e x t N o d e ( t ) ; i f ( " s t r i n g " = = t y p e o f r ) f o r ( k e y i n n = d o c u m e n t . c r e a t e E l e m e n t ( r ) , o ) " o " = = k e y [ 0 ] & & " n " = = k e y [ 1 ] ? n . a d d E v e n t L i s t e n e r ( k e y . t o L o w e r C a s e ( ) . s u b s t r i n g ( 2 ) , o [ k e y ] ) : " c l a s s " = = = k e y ? n.className=o[key]:n.setAttribute(key,o[key]);else if("function"==typeof r){var a=new r(o);n=e(a.render(a.props,a.state)),a.base=n}return(i|| []).forEach(function(t){return n.appendChild(e(t))}),n},p=function(e,t){u(void 0,e,t)};p(i(l),document.body); },{}]},{},[4]) app.min.js vDOM example on Codepen
  121. export default class App extends Component { ... render(props, {

    highlighted }) { return ( <button type="button" highlighted={highlighted} onclick={this.toggleHighlight}> { highlighted ? 'Please stop!': 'Highlight me!' } </button> ); } } codesandbox.io/s/rwjol2n3op
  122. codesandbox.io/s/rwjol2n3op export default class App extends Component { ... render(props,

    { highlighted }) { return React.createElement( 'button', { type: 'button', highlighted: highlighted, onclick: this.toggleHighlight }, highlighted ? 'Please stop!' : 'Highlight me!' ); } }
  123. codesandbox.io/s/rwjol2n3op export default class App extends Component { ... render(props,

    { highlighted }) { return React.createElement( 'button', { type: 'button', highlighted: highlighted, onclick: this.toggleHighlight }, highlighted ? 'Please stop!' : 'Highlight me!' ); } } It's "just" function calls
  124. ./app.js const diff = (dom, vnode) => { // compare

    type // compare attributes // compare children }
  125. import { html, render } from 'lit-html'; const data =

    { list: ['"', ..., '+'] }; const App = ({list}) => { return html` <div class="app"> <h1>! Frontend Con 2018! !</h1> <ul> ${ list.map(item => html`<li>${ item }</li>`) } </ul> </div> ` };
  126. import { html, render } from 'lit-html'; const data =

    { list: ['"', ..., '+'] }; const App = ({list}) => { return html` <div class="app"> <h1>! Frontend Con 2018! !</h1> <ul> ${ list.map(item => html`<li>${ item }</li>`) } </ul> </div> ` }; render(App(data), document.body);
  127. html`<li>${ '"' } ${ Math.random() }</li>` function html(chunks, ...interpolations) {

    // diff only things that can change } [['<li>', ' ', '</li>' ], '"', 0.62...]
  128. html`<li>${ '"' } ${ Math.random() }</li>` function html(chunks, ...interpolations) {

    // diff only things that can change } [['<li>', ' ', '</li>' ], '"', 0.62...]
  129. html`<li>${ '"' } ${ Math.random() }</li>` function html(chunks, ...interpolations) {

    // diff only things that can change } [['<li>', ' ', '</li>' ], '"', 0.62...]