-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactAPI.js
More file actions
96 lines (76 loc) · 2.05 KB
/
Copy pathreactAPI.js
File metadata and controls
96 lines (76 loc) · 2.05 KB
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
/**
* @name FakeLibreryReact
* @author Dedaldino Daniel
* @description "Reverse React API Engineering"
*/
/**
* @function ConverteHTML
* @param {*} virtualNode
*/
function converteHTML(virtualNode) {
if( typeof virtualNode === 'string' || typeof virtualNode === 'number') {
return document.createTextNode(`${virtualNode}`);
}
const $domElement = document.createElement(virtualNode.tagName);
if(virtualNode.props.className !== undefined) {
$domElement.className = virtualNode.props.className;
}
if(virtualNode.props.id !== undefined) {
$domElement.id = virtualNode.props.id;
}
if(virtualNode.props.value !== undefined) {
$domElement.value = virtualNode.props.value;
}
virtualNode.props.children.forEach((virtualChild) => {
$domElement.appendChild(converteHTML(virtualChild));
})
return $domElement;
}
/**
* @funtion render
* @param {*} initalVirtualTree
* @param {*} $domRoot
*/
function render(initalVirtualTree, $domRoot) {
const $appHTML = converteHTML(initalVirtualTree);
$domRoot.appendChild($appHTML);
}
/**
* @function createElement
* @param {*} elementType
* @param {*} props
* @param {...any} children
*/
function createElement(elementType, props, ...children) {
const virtualElementProps = {
...props,
children
}
if(typeof elementType === "function") {
return elementType(virtualElementProps);
}
return {
tagName: elementType,
props: virtualElementProps
};
}
/**
* @libriry React
*/
const React = {
createElement,
};
//* Using *//
function App() {
return (
<section className= 'App' id="main-section">
<h1>Contador Jsx</h1>
<div>
<div>0</div>
<button className="btn-incrment" value="23">Incrementar</button>
<button className="btn-decrement">Decrementar</button>
</div>
</section>
);
}
render(React.createElement(App, null), document.querySelector('#root'));