const React = {
createElement: (tag, props, ...children) => {
if (typeof tag === 'function') {
return tag(props);
}
const element = { tag, props: { ...props, children } };
return element;
}
};
const ReactDOM = {
render: (element, container) => {
if (['string', 'number'].includes(typeof element)) {
container.appendChild(document.createTextNode(String(element)));
return;
}
const actualElement = document.createElement(element.tag);
if (element.props) {
Object.keys(element.props)
.filter(p => p !== 'children')
.forEach(p => actualElement[p] = element.props[p]);
}
if (element.props.children) {
element.props.children.forEach(child => ReactDOM.render(child, actualElement));
}
container.appendChild(actualElement);
}
};
const App = () => {
return React.createElement('div', null,
React.createElement('h1', null, 'Hello, React!'),
React.createElement('p', null, 'This is a simple React implementation.')
);
};
ReactDOM.render(React.createElement(App), document.getElementById('root'));