Skip to content

Instantly share code, notes, and snippets.

@zenius
Created June 2, 2019 07:38
Show Gist options
  • Save zenius/a622acb5125448fcb2ce4e480104dc7e to your computer and use it in GitHub Desktop.
Save zenius/a622acb5125448fcb2ce4e480104dc7e to your computer and use it in GitHub Desktop.
const Feact = {
createElement(type, props, children) {
const element = {
type,
props: props || {}
};
if (children) {
element.props.children = children;
}
return element;
},
render(element, container) {
const componentInstance = new FeactDOMComponent(element);
return componentInstance.mountComponent(container);
}
};
Elements are just simple objects representing something we want rendered.
class FeactDOMComponent {
constructor(element) {
this._currentElement = element;
}
mountComponent(container) {
const domElement =
document.createElement(this._currentElement.type);
const text = this._currentElement.props.children;
const textNode = document.createTextNode(text);
domElement.appendChild(textNode);
container.appendChild(domElement);
this._hostNode = domElement;
return domElement;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment