1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| ReactDOM.render = function render(obj, container, callback) { let { type, props } = obj, element if (typeof type === 'string') { element = document.createElement(type) each(props, (value, key) => { if (key === 'className') { element.setAttribute('class', value) return } if (key === 'style') { each(value, (item, attr) => { element.style[attr] = item }) return } if (key === 'children') { if (!Array.isArray(value)) value = [value] value.forEach((item, index) => { if (typeof item === 'string') { let textNode = document.createTextNode(item) element.appendChild(textNode) return } render(item, element) }) return } element.setAttribute(key, value) }) container.appendChild(element) if (typeof callback === 'function') { callback() } return } }
|