-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathReactDOMMini.js
107 lines (96 loc) · 2.27 KB
/
ReactDOMMini.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import ReactReconciler from 'react-reconciler';
let reconciler = ReactReconciler({
/* configuration for how to talk to the host environment */
/* aka "host config" */
supportsMutation: true,
createInstance(
type,
props,
rootContainerInstance,
hostContext,
internalInstanceHandle,
) {
let el = document.createElement(type);
['alt', 'className', 'href', 'rel', 'src', 'target'].forEach(k => {
if (props[k]) el[k] = props[k];
});
if (props.onClick) {
el.addEventListener('click', props.onClick);
}
if (props.bgColor) {
el.style.backgroundColor = props.bgColor;
}
return el;
},
createTextInstance(
text,
rootContainerInstance,
hostContext,
internalInstanceHandle,
) {
return document.createTextNode(text);
},
appendChildToContainer(container, child) {
container.appendChild(child);
},
appendChild(parent, child) {
parent.appendChild(child);
},
appendInitialChild(parent, child) {
parent.appendChild(child);
},
removeChildFromContainer(container, child) {
container.removeChild(child);
},
removeChild(parent, child) {
parent.removeChild(child);
},
insertInContainerBefore(container, child, before) {
container.insertBefore(child, before);
},
insertBefore(parent, child, before) {
parent.insertBefore(child, before);
},
prepareUpdate(
instance,
type,
oldProps,
newProps,
rootContainerInstance,
currentHostContext,
) {
let payload;
if (oldProps.bgColor !== newProps.bgColor) {
payload = {newBgColor: newProps.bgColor};
}
return payload;
},
commitUpdate(
instance,
updatePayload,
type,
oldProps,
newProps,
finishedWork,
) {
if (updatePayload.newBgColor) {
instance.style.backgroundColor = updatePayload.newBgColor;
}
},
finalizeInitialChildren() {},
getChildHostContext() {},
getPublicInstance() {},
getRootHostContext() {},
prepareForCommit() {},
resetAfterCommit() {},
shouldSetTextContent() {
return false;
},
});
let ReactDOMMini = {
render(whatToRender, div) {
let container = reconciler.createContainer(div, false, false);
reconciler.updateContainer(whatToRender, container, null, null);
},
};
export default ReactDOMMini;