Skip to content

Commit 0627d86

Browse files
author
gourav b
committed
implment virtual dom to conditionally render app components:
in app() -aft currentView: set rootNode to res createElm passing in currentView -pass rootNode to appendChild in dispatch() -aft updtdVw: set patches to diff btwn current & updated views -set rootNode to res of patch, applying patches to rootNode -rm: replaceChild call on node
1 parent b52a38a commit 0627d86

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

  • functional-programming/src/moore-james-udemy/src/counter/src

functional-programming/src/moore-james-udemy/src/counter/src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import h from 'hyperscript';
21
import hh from 'hyperscript-helpers';
2+
import { h, diff, patch } from 'virtual-dom';
3+
import createElement from 'virtual-dom/create-element';
34

45
const { div, button } = hh(h);
56

@@ -34,15 +35,19 @@ function update(msg, model) {
3435
// impure code below
3536

3637
function app(initModel, update, view, node){
38+
39+
// sets up initial app state
3740
let model = initModel;
3841
let currentView = view(dispatch, model);
39-
node.appendChild(currentView);
42+
let rootNode = createElement(currentView);
43+
node.appendChild(rootNode);
4044

4145
// handles update sequence on app interaction
4246
function dispatch(msg){
4347
model = update(msg, model);
4448
const updatedView = view(dispatch, model);
45-
node.replaceChild(updatedView, currentView);
49+
const patches = diff(currentView, updatedView);
50+
rootNode = patch(rootNode, patches);
4651
currentView = updatedView;
4752
}
4853
}

0 commit comments

Comments
 (0)