fake React, render() #1
by city41
HTML
<div id="root">
</div>
JavaScript
const FeactInstanceMap = {
set(key, value) {
key.__feactInternalInstance = value;
},
get(key) {
return key.__feactInternalInstance;
}
};
class FeactDOMComponent {
constructor(element) {
this._currentElement = element;
}
mountComponent(container) {
const domElement = document.createElement(this._currentElement.type);
const textNode = document.createTextNode(this._currentElement.props.children);
domElement.appendChild(textNode);
container.appendChild(domElement);
this._hostNode = domElement;
return domElement;
}
receiveComponent(nextElement) {
const prevElement = this._currentElement;
this.updateComponent(prevElement, nextElement);
}
updateComponent(prevElement, nextElement) {
const lastProps = prevElement.props;
const nextProps = nextElement.props;
this._updateDOMProperties(lastProps, nextProps);
this._updateDOMChildren(lastProps, nextProps);
}
_updateDOMProperties(lastProps, nextProps) {
// nothing to do! I'll explain why below
}
_updateDOMChildren(lastProps, nextProps) {
const lastContent = lastProps.children;
const nextContent = nextProps.children;
if (!nextContent) {
this.updateTextContent('');
} else if (lastContent !== nextContent) {
this.updateTextContent('' + nextContent);
}
}
updateTextContent(content) {
const node = this._hostNode;
node.textContent = content;
const firstChild = node.firstChild;
if (firstChild && firstChild === node.lastChild
&& firstChild.nodeType === 3) {
firstChild.nodeValue = content;
return;
}
node.textContent = content;
}
}
class FeactCompositeComponentWrapper {
constructor(element) {
this._currentElement = element;
}
mountComponent(container) {
const...