|
1 | 1 | import React from 'react'; // 虽然也许可能没有使用,如果删了就会报错React.createElement |
2 | 2 | import ReactDOM from 'react-dom'; |
3 | 3 |
|
4 | | -import ClassCompRender from './test/ClassCompRender' |
| 4 | +// import ClassCompRender from './test/ClassCompRender' |
5 | 5 |
|
6 | 6 | // import './test/test1' |
7 | 7 | // import './test/toggleImg' |
@@ -63,60 +63,60 @@ import App from './App' |
63 | 63 | ReactDOM.render(<App />, document.getElementById('root')) |
64 | 64 |
|
65 | 65 |
|
66 | | -const app = <div className="prop"> |
67 | | - <h1> |
68 | | - 标题 |
69 | | - {["abc", null, <p>段落</p>]} |
70 | | - </h1> |
71 | | - <p> |
72 | | - {undefined} |
73 | | - </p> |
74 | | - <ClassCompRender /> |
75 | | -</div>; |
76 | | - |
77 | | -function setProps(dom, props = {}) { |
78 | | - for (const [key, value] of Object.entries(props)) { |
79 | | - if (key === 'children' || value == null) continue; |
80 | | - |
81 | | - if (key === 'className') { |
82 | | - dom.setAttribute('class', value); |
83 | | - } else if (key === 'style' && typeof value === 'object') { |
84 | | - Object.assign(dom.style, value); |
85 | | - } else if (/^on[A-Z]/.test(key) && typeof value === 'function') { |
86 | | - const eventName = key.slice(2).toLowerCase(); |
87 | | - dom.addEventListener(eventName, value); |
88 | | - } else if (key in dom) { |
89 | | - dom[key] = value; |
90 | | - } else { |
91 | | - dom.setAttribute(key, String(value)); |
92 | | - } |
93 | | - } |
94 | | -} |
95 | | -function render(vnode, container) { |
96 | | - if (Array.isArray(vnode)) { |
97 | | - vnode.forEach(child => render(child, container)); |
98 | | - return; |
99 | | - } |
100 | | - |
101 | | - if (vnode == null || typeof vnode === 'boolean') return; |
102 | | - |
103 | | - if (typeof vnode === 'string' || typeof vnode === 'number') { |
104 | | - container.appendChild(document.createTextNode(String(vnode))); |
105 | | - return; |
106 | | - } |
107 | | - |
108 | | - if (typeof vnode.type === 'function') { |
109 | | - render(vnode.type(vnode.props || {}), container); |
110 | | - return; |
111 | | - } |
112 | | - |
113 | | - if (!vnode || typeof vnode !== 'object' || !vnode.type) return; |
114 | | - |
115 | | - const dom = document.createElement(vnode.type); |
116 | | - setProps(dom, vnode.props); |
117 | | - render(vnode.props?.children, dom); // 直接递归,不手动包一层 list |
118 | | - container.appendChild(dom); |
119 | | -} |
| 66 | +// const app = <div className="prop"> |
| 67 | +// <h1> |
| 68 | +// 标题 |
| 69 | +// {["abc", null, <p>段落</p>]} |
| 70 | +// </h1> |
| 71 | +// <p> |
| 72 | +// {undefined} |
| 73 | +// </p> |
| 74 | +// <ClassCompRender /> |
| 75 | +// </div>; |
| 76 | + |
| 77 | +// function setProps(dom, props = {}) { |
| 78 | +// for (const [key, value] of Object.entries(props)) { |
| 79 | +// if (key === 'children' || value == null) continue; |
| 80 | + |
| 81 | +// if (key === 'className') { |
| 82 | +// dom.setAttribute('class', value); |
| 83 | +// } else if (key === 'style' && typeof value === 'object') { |
| 84 | +// Object.assign(dom.style, value); |
| 85 | +// } else if (/^on[A-Z]/.test(key) && typeof value === 'function') { |
| 86 | +// const eventName = key.slice(2).toLowerCase(); |
| 87 | +// dom.addEventListener(eventName, value); |
| 88 | +// } else if (key in dom) { |
| 89 | +// dom[key] = value; |
| 90 | +// } else { |
| 91 | +// dom.setAttribute(key, String(value)); |
| 92 | +// } |
| 93 | +// } |
| 94 | +// } |
| 95 | +// function render(vnode, container) { |
| 96 | +// if (Array.isArray(vnode)) { |
| 97 | +// vnode.forEach(child => render(child, container)); |
| 98 | +// return; |
| 99 | +// } |
| 100 | + |
| 101 | +// if (vnode == null || typeof vnode === 'boolean') return; |
| 102 | + |
| 103 | +// if (typeof vnode === 'string' || typeof vnode === 'number') { |
| 104 | +// container.appendChild(document.createTextNode(String(vnode))); |
| 105 | +// return; |
| 106 | +// } |
| 107 | + |
| 108 | +// if (typeof vnode.type === 'function') { |
| 109 | +// render(vnode.type(vnode.props || {}), container); |
| 110 | +// return; |
| 111 | +// } |
| 112 | + |
| 113 | +// if (!vnode || typeof vnode !== 'object' || !vnode.type) return; |
| 114 | + |
| 115 | +// const dom = document.createElement(vnode.type); |
| 116 | +// setProps(dom, vnode.props); |
| 117 | +// render(vnode.props?.children, dom); // 直接递归,不手动包一层 list |
| 118 | +// container.appendChild(dom); |
| 119 | +// } |
120 | 120 | // const root = document.getElementById('root'); |
121 | 121 | // render(app, root); |
122 | 122 |
|
|
0 commit comments