-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-start-0.html
255 lines (213 loc) · 7.95 KB
/
react-start-0.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<meta charset="utf-8">
<title>Reactjs in 40 </title>
<style media="screen">
.like-btn-a { font-size: 50px; }
</style>
</head>
<body>
<div class="wrapper_0">
<button class='like-btn'>
<span class='like-text'>点赞</span>
<span>👍</span>
</button>
</div>
<div class="wrapper_1">
</div>
<div class="wrapper_2_1">
</div>
<div class="wrapper_a">
</div>
<div class='wrapper'>
<button class='like-btn'>
<span class='like-text'>点赞</span>
<span>👍</span>
</button>
<script type="text/javascript">
class LikeButton {
render () {
return `
<button id='like-btn'>
<span class='like-text'>赞</span>
<span>👍</span>
</button>
`
}
}
class LikeButton2{
render(){
this.el=createDOM
}
}
const button = document.querySelector('.like-btn')
const buttonText = document.querySelector('.like-text')
let isLiked = false
button.addEventListener('click',()=>{
isLiked = !isLiked
if(isLiked){
buttonText.innerHTML = 'cancel'
}else{
buttonText.innerHTML = 'ok'
}
},false)
const wrapper = document.querySelector('.wrapper')
const likeButton1 = new LikeButton()
wrapper.innerHTML = likeButton1.render()
console.log('1')
const likeButton2 = new LikeButton()
wrapper.innerHTML+=likeButton2.render()
console.log('2')
class LikeButtonDom{
constructor(){
this.state = {isLiked:false}
}
changeLikeText(){
const likeText = this.el.querySelector('.like-text')
this.state.isLiked = !this.state.isLiked
likeText.innerHTML = this.state.isLiked?'取消':'点赞'
}
render(){
this.el = createDOMFromString(
`
<button class='like-button'>
<span class='like-text'>点赞赞</span>
<span>👍</span>
</button>
`)
this.el.addEventListener('click',this.changeLikeText.bind(this),false)
return this.el;
}
}
const createDOMFromString = (domString)=>{
const div = document.createElement('div')
div.innerHTML = domString
return div
}
const wrapper_1 = document.querySelector('.wrapper_1')
const likeButton_1 = new LikeButtonDom()
wrapper_1.appendChild(likeButton_1.render())
const likeButton_2 = new LikeButtonDom()
wrapper_1.appendChild(likeButton_2.render())
class LikeButton_2_1{
constructor(){
this.state = {isLiked:false}
}
setState(state){
const oldEl = this.el
this.state = state
this.el = this.render()
if(this.onStateChagne)this.onStateChagne(oldEl,this.el)
}
changeLikeText(){
this.setState({
isLiked:!this.state.isLiked
})
}
render(){
this.el = createDOMFromString(
`
<button class='like-btn'>
<span class='like-text'>${this.state.isLiked?'取消':'点赞'}</span>
<span>👍👍</span>
</button>
`
)
this.el.addEventListener('click',this.changeLikeText.bind(this),false)
return this.el
}
}
const wrapper_2_1 = document.querySelector('.wrapper_2_1')
const likeButton_2_1 = new LikeButton_2_1()
wrapper_2_1.appendChild(likeButton_2_1.render())
likeButton_2_1.onStateChagne = (oldEl,newEl)=>{
wrapper_2_1.insertBefore(newEl,oldEl)
wrapper_2_1.removeChild(oldEl)
}
// 组件
class Component{
constructor(props={}){
this.props = props
}
setState(state){
// 获取老的dom
const oldEl = this.el
// 记录新的状态
this.state = state
// 执行dom渲染操作
this._renderDOM()
if(this.onStateChange) this.onStateChange(oldEl,this.el)
}
_renderDOM(){
// 根据子类的render返回的字符串信息生成新的dom
this.el = createDOMFromString(this.render())
// 如果当前组件存在onClick事件,则向dom中添加onClick事件的回调
if(this.onClick){
this.el.addEventListener('click',this.onClick.bind(this),false)
}
// 返回新的dom
return this.el
}
}
const mount = (component,wrapper)=>{
wrapper.appendChild(component._renderDOM())
component.onStateChange = (oldEl,newEl)=>{
wrapper.insertBefore(newEl,oldEl)
wrapper.removeChild(oldEl)
}
}
class LikeButtonSub extends Component{
constructor(props){
super(props)
this.state = {isLiked:false}
}
onClick(){
this.setState({
isLiked:!this.state.isLiked
})
}
render(){
return
`
<button class='like-btn-a' style="background-color:${this.props.bgColor}">
<span class='like-text-a'>
${this.state.isLiked?'取消':'点赞'}
</span>
<span>👍</span>
</button>
`
}
}
class RedBlueButton extends Component{
constructor(props){
super(props)
this.state={
color:'red'
}
}
onClick(){
this.setState({
color:'blue'
})
}
render(){
return
`
<div style='color:${this.state.color};'>${this.state.color}</div>
`
}
}
const _wrapper_a = document.querySelector('.wrapper_a')
mount(new LikeButtonSub({bgColor:'red'}),_wrapper_a)
mount(new LikeButtonSub(),_wrapper_a)
mount(new RedBlueButton(),_wrapper_a)
</script>
</div>
</body>
</html>