Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. class Component {}
  2. const React = {
  3. Component
  4. };
  5.  
  6. function thisIsMyAwesomeFunction(elementTagName, props, ...children) {
  7. children = [].concat(...children);
  8. return {
  9. elementTagName,
  10. props,
  11. children
  12. };
  13. }
  14.  
  15. function render(virtualDom): HTMLElement {
  16. let dom: HTMLElement = document.createElement(virtualDom.elementTagName);
  17. const props = virtualDom.props || {};
  18. for (let key in props) {
  19. dom.setAttribute(key, virtualDom.props[key]);
  20. }
  21.  
  22. for (let child of virtualDom.children) {
  23. if (typeof child === 'string') {
  24. dom.appendChild(document.createTextNode(child));
  25. } else if (typeof child.elementTagName === 'function') {
  26. const childVirtualDom = child.elementTagName(child.props);
  27. dom.appendChild(render(childVirtualDom));
  28. } if (child.elementTagName.prototype === React.Component) {
  29. const instance = new child.elementTagName(child.props);
  30. const classVirtualDom = instance.render();
  31. instance.componentWillMount();
  32. dom.appendChild(render(classVirtualDom));
  33. instance.componentDidMount();
  34. } else {
  35. dom.appendChild(render(child));
  36. }
  37. }
  38. return dom;
  39. }
  40.  
  41. const ReactDOM = {
  42. render
  43. }
  44.  
  45. /**
  46. * Doc
  47. * @param props Some props
  48. */
  49. const Items = (props) => {
  50. return (
  51. <ul>
  52. {props.values.map(value => <li>{value}</li>)}
  53. </ul>
  54. );
  55. };
  56.  
  57. class MyComponent extends React.Component {
  58. render() {
  59. return (
  60. <div>
  61. <h1>Reactless JSX</h1>
  62. <p>This is a test of JSX outside of React</p>
  63. <Items values={['First', 'Second', 'Third']}/>
  64. </div>
  65. );
  66. }
  67. }
  68. const virtualDom = new MyComponent().render();
  69. const dom = ReactDOM.render(virtualDom);
  70. document.body.appendChild(dom);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement