-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-dom.tsx
39 lines (32 loc) · 1.01 KB
/
react-dom.tsx
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
import { IGlobalState, JSX } from "./types";
import React from "./react";
import App from "./App";
const ReactDOM = {
render: (element: JSX, container: HTMLElement): void => {
if (typeof element === "string" || typeof element === "number") {
container.appendChild(document.createTextNode(String(element)));
return;
}
const { props, tag } = element;
const domElement = document.createElement(tag);
if (props.children) {
for (const child of props.children) {
ReactDOM.render(child, domElement);
}
}
for (const prop in props) {
const value = props[prop];
if (prop !== "children") {
domElement[prop.toLowerCase()] = value;
}
}
container.appendChild(domElement);
},
rerender: (globalState: IGlobalState) => {
const rootContainer = document.getElementById("root");
rootContainer.removeChild(rootContainer.firstChild);
globalState.cursor = 0;
ReactDOM.render(<App />, rootContainer);
},
};
export default ReactDOM;