Here's the code on how you can build your own react. So you can understand how react works.
Last active
September 20, 2023 03:12
-
-
Save warengonzaga/094b79d1aad74bf20627014f1bfd8ed6 to your computer and use it in GitHub Desktop.
React - Plain Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<div id="root"></div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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