Jul 14, 2024
react.js
file and an index.html
file.index.html
structure with a root div:
<html>
<head>
<title>CustomReact App</title>
</head>
<body>
<div id="root"></div>
<script src="customReact.js"></script>
</body>
</html>
``
const root = document.getElementById("root");
function render(element, container) {
container.appendChild(element);
}
function createReactElement(type, props, ...children) {
const element = document.createElement(type);
for (const [name, value] of Object.entries(props)) {
element.setAttribute(name, value);
}
children.forEach(child => {
if (typeof child === "string") {
element.appendChild(document.createTextNode(child));
} else {
element.appendChild(child);
}
});
return element;
}
const customReactElement = createReactElement("a", { href: "http://google.com", target: "_blank" }, "Click me to visit Google");
render(customReactElement, root);