Skip to content

Instantly share code, notes, and snippets.

@warengonzaga
Last active September 20, 2023 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warengonzaga/094b79d1aad74bf20627014f1bfd8ed6 to your computer and use it in GitHub Desktop.
Save warengonzaga/094b79d1aad74bf20627014f1bfd8ed6 to your computer and use it in GitHub Desktop.
React - Plain Javascript

Build Your Own React

Here's the code on how you can build your own react. So you can understand how react works.

<html>
<body>
<div id="root"></div>
</body>
</html>
function render(reactElement, containerDOMElement) {
const element = document.createElement(reactElement.type);
element.innerText = reactElement.children;
for (const key in reactElement.props) {
const value = reactElement.props[key];
element.setAttribute(key, value);
}
containerDOMElement.appendChild(element);
}
const reactElement = {
type: 'a',
props: {
href: 'https://warengonzaga.com/',
},
children: 'Visit my website!',
};
const containerDOMElement =
document.querySelector('#root');
render(reactElement, containerDOMElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment