This file contains 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
const element = { | |
type: "div", | |
props: { | |
id: "container", | |
children: [ | |
{ type: "input", props: { value: "foo", type: "text" } }, | |
{ type: "a", props: { href: "/bar" } }, | |
{ type: "span", props: {} } | |
] | |
} |
This file contains 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
<div id="container"> | |
<input value="foo" type="text"> | |
<a href="/bar"></a> | |
<span></span> | |
</div> |
This file contains 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(element, parentDom) { | |
const { type, props } = element; | |
const dom = document.createElement(type); | |
const childElements = props.children || []; | |
childElements.forEach(childElement => render(childElement, dom)); | |
parentDom.appendChild(dom); | |
} |
This file contains 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(element, parentDom) { | |
const { type, props } = element; | |
const dom = document.createElement(type); | |
const isListener = name => name.startsWith("on"); | |
Object.keys(props).filter(isListener).forEach(name => { | |
const eventType = name.toLowerCase().substring(2); | |
dom.addEventListener(eventType, props[name]); | |
}); |
This file contains 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
const reactElement = { | |
type: "span", | |
props: { | |
children: ["Foo"] | |
} | |
}; |
This file contains 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
const textElement = { | |
type: "span", | |
props: { | |
children: [ | |
{ | |
type: "TEXT ELEMENT", | |
props: { nodeValue: "Foo" } | |
} | |
] | |
} |
This file contains 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(element, parentDom) { | |
const { type, props } = element; | |
// Create DOM element | |
const isTextElement = type === "TEXT ELEMENT"; | |
const dom = isTextElement | |
? document.createTextNode("") | |
: document.createElement(type); | |
// Add event listeners |
This file contains 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
const element = { | |
type: "div", | |
props: { | |
id: "container", | |
children: [ | |
{ type: "input", props: { value: "foo", type: "text" } }, | |
{ | |
type: "a", | |
props: { | |
href: "/bar", |
This file contains 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
const element = ( | |
<div id="container"> | |
<input value="foo" type="text" /> | |
<a href="/bar">bar</a> | |
<span onClick={e => alert("Hi")}>click me</span> | |
</div> | |
); |
This file contains 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 createElement(type, config, ...args) { | |
const props = Object.assign({}, config); | |
const hasChildren = args.length > 0; | |
props.children = hasChildren ? [].concat(...args) : []; | |
return { type, props }; | |
} |