Skip to content

Commit 4a15aa7

Browse files
Make own React Library
1 parent 3ae7b43 commit 4a15aa7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Diff for: custom_React/custom-react.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function customRender(reactElement, container) {
2+
// this is not best way beacuse every time use to setAttribute for every element
3+
4+
// const domElement = document.createElement(reactElement.type);
5+
// domElement.innerHTML = reactElement.children;
6+
// domElement.setAttribute("href", reactElement.props.herf);
7+
// domElement.setAttribute("target", reactElement.children.target);
8+
// container.appendChild(domElement);
9+
10+
// loop base code , best way
11+
12+
const domElement = document.createElement(reactElement.type);
13+
domElement.innerHTML = reactElement.children;
14+
for (const prop in reactElement.props) {
15+
if (prop == "children") continue; // if prop attr have h children element
16+
domElement.setAttribute(prop, reactElement.props[prop]);
17+
}
18+
container.appendChild(domElement);
19+
}
20+
21+
const reactElement = {
22+
type: "a",
23+
props: {
24+
herf: "https://google.com",
25+
target: "_blank",
26+
},
27+
children: "Visit Google",
28+
}; // react give same like this end of the day
29+
30+
const mainContainer = document.querySelector("#root");
31+
32+
customRender(reactElement, mainContainer);

Diff for: custom_React/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Custom React App</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
11+
<script src="./custom-react.js"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)