1
- export function render ( element , parentDom ) {
1
+ let rootInstance = null ;
2
+
3
+ export function render ( element , container ) {
4
+ const prevInstance = rootInstance ;
5
+ const nextInstance = reconcile ( container , prevInstance , element ) ;
6
+ rootInstance = nextInstance ;
7
+ }
8
+
9
+ function reconcile ( parentDom , instance , element ) {
10
+ if ( instance == null ) {
11
+ const newInstance = instantiate ( element ) ;
12
+ parentDom . appendChild ( newInstance . dom ) ;
13
+ return newInstance ;
14
+ } else {
15
+ const newInstance = instantiate ( element ) ;
16
+ parentDom . replaceChild ( newInstance . dom , instance . dom ) ;
17
+ return newInstance ;
18
+ }
19
+ }
20
+
21
+ function instantiate ( element ) {
2
22
const { type, props } = element ;
3
23
4
24
// Create DOM element
@@ -20,10 +40,12 @@ export function render(element, parentDom) {
20
40
dom [ name ] = props [ name ] ;
21
41
} ) ;
22
42
23
- // Render children
43
+ // Instantiate and append children
24
44
const childElements = props . children || [ ] ;
25
- childElements . forEach ( childElement => render ( childElement , dom ) ) ;
45
+ const childInstances = childElements . map ( instantiate ) ;
46
+ const childDoms = childInstances . map ( childInstance => childInstance . dom ) ;
47
+ childDoms . forEach ( childDom => dom . appendChild ( childDom ) ) ;
26
48
27
- // Append to parent
28
- parentDom . appendChild ( dom ) ;
49
+ const instance = { dom , element , childInstances } ;
50
+ return instance ;
29
51
}
0 commit comments