createElement
render
function render(vdom, container) {
mount(vdom, container)
}
function mount(vdom, parentDOM) {
const newDOM = createDom(vdom);
parentDOM.appendChild(newDom);
}
function createDom(vdom) {
let { type, props } = vdom;
let dom;
if(type === REACT_TEXT) {
dom = document.createTextNode(props.content)
}else {
dom = document.createElement(type)
}
if(props) {
updateProps(dom, {}, props);
let children = props.children;
if(typeof children === 'object' && children.type) {
render(children.dom)
}else if(Array.isArray(children)) {
reconcileChidlren(children, dom)
}
}
vdom.dom = dom;
return vdom;
}
function reconcileChidlren(children, parentDom) {
children.forEach(childVdom => render(childVdom, parentDom));
}
function updateProps(dom, oldProps, newProps) {
for (let key in newProps) {
if (key === 'children') {
continue;
} else if (key === 'style') {
let styleObj = newProps[key];
for (let attr in styleObj) {
dom.style[attr] = styleObj[attr];
}
} else {
dom[key] = newProps[key]
}
}
}