-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path03-function-component-and-class-component.html
175 lines (163 loc) · 4.56 KB
/
03-function-component-and-class-component.html
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>03-function-component-and-class-component copy</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/babel.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
// 3 函数组件,类组件
// 函数组件转类组件,然后实例化,设置props,调用render方法(函数被封装成render函数)返回jsx,使用_render渲染jsx
/*
函数组件 大写开头
function Home(){
reuturn <div />
}
<Home />
类组件 大写开头
class Home extends React.Component{
render(){
return (
<div />
)
}
}
*/
/*
函数组件返回JSX,类组件render返回jsx,
函数组件封住成类组件,统一
*/
// 统一的组件继承此,必须放到React上面
class Component{
constructor(props={}){
this.props = props
this.state = {}
}
}
const React = {
createElement,
Component
}
const ReactDom = {
render
}
// function Home(){
// return (
// <div className="active" title="123">
// Hello,
// <span>React</span>
// </div>
// )
// }
class Home extends React.Component{
render(){
return (
<div className="active" title="123">
Hello,
<span>React</span>
</div>
)
}
}
//
function createElement(tag, attrs, ...childrens) {
return {
tag,
attrs,
childrens
}
}
function render(vnode,container) {
return container.appendChild(_render(vnode))
}
// jsx转dom
function _render(vnode){
if(vnode === undefined || vnode === null || typeof vnode === 'boolean') vnode = ''
if(typeof vnode === 'string'){
return document.createTextNode(vnode)
}
// 如果tag是函数,渲染组件
if(typeof vnode.tag === 'function') {
// 1.创建组件
const comp = createComponent(vnode.tag,vnode.attrs)
// 2.设置组件的属性
setComponentProps(comp,vnode.attrs)
// 3.组件渲染的节点对象返回
return comp.base
}
const {tag,attrs,childrens} = vnode
const dom = document.createElement(tag)
if(attrs){
Object.keys(attrs).forEach(key=>{
const value = attrs[key]
setAttribute(dom,key,attrs[key])
})
}
childrens.forEach(child=>{
dom.appendChild(_render(child))
})
return dom
}
// 创建组件,封装函数组件和类组件一样的class形式,然后返回实例,但是没有调用render方法,typeof class ===‘function’
function createComponent(comp,props){
let inst
// 判断类组件或者函数组件
if(comp.prototype && comp.prototype.render){
inst = new comp(props)
}else{
// 函数组件扩展成类组件
inst = new Component(props)
inst.constructor = comp
inst.render = function(){
return this.constructor(props)
}
}
return inst
}
// 设置完成props,调用组件实例的render方法返回jsx,使用_render渲染jsx
function renderComponent(comp){
let vnode = comp.render(comp.props)
comp.base = _render(comp.render(comp.props))
}
// 设置组件实例的属性
function setComponentProps(comp,props){
comp.props = props
renderComponent(comp)
}
function setAttribute(dom,key,value){
if(key==='className'){
key = 'class'
}
if(/on\w+/.test(key)){
key = key.toLowerCase()
dom[key] = value || ''
}else if(key === 'style'){
if(!value || typeof key === 'string'){
dom.style.cssText = value || ''
}else if(value && typeof key === 'object'){
for(let k in value){
if(typeof value[k] === 'number'){
dom.style[k] = value[k] + 'px'
}else{
dom.style[k] = value[k]
}
}
}
}else{
if(key in dom){
dom[key] = value || ''
}
if(value){
dom.setAttribute(key,value)
}else{
dom.removeAttribute(key)
}
}
}
ReactDom.render(<Home name="panda"/>, document.getElementById('root'))
</script>
</body>
</html>