-
Notifications
You must be signed in to change notification settings - Fork 0
/
react101.html
142 lines (137 loc) · 3.7 KB
/
react101.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="wrapper">
<!-- <button class="like-btn">
<span class="like-text">点赞</span>
</button> -->
</div>
<script>
const createDOMFromString = (domString) => {
const div = document.createElement('div')
div.innerHTML = domString
return div
}
// class LikeButton {
// constructor(){
// this.state = {isLiked:false}
// }
// changeLikeText(){
// const likeText = this.el.querySelector('.like-text')
// this.state.isLiked = !this.state.isLiked
// likeText.innerHTML = this.state.isLiked ? 'cancel' : "confirm"
// }
// render(){
// this.el = createDOMFromString(
// `
// <button id='like-btn'>
// <span>赞</span>
// </button>
// `)
// this.el.addEventListener('click',()=>{
// console.log('click')
// },false)
// return this.el
// }
// }
const wrapper = document.querySelector('.wrapper')
// const likeButton1 = new LikeButton()
// wrapper.appendChild(likeButton1.render())
// const likeButton2 = new LikeButton()
// wrapper.appendChild(likeButton1.render())
class LikeButton{
constructor(){
this.state = {
isLiked:false
}
}
setState (state){
const oldEl = this.el
this.state = state
this.el = this.render()
if(this.onStateChange){
this.onStateChange(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 ? "cancel":'cheer'}</span>
</button>
`)
this.el.addEventListener("click",()=>{
this.changeLikeText()
})
return this.el
}
}
const likeButton = new LikeButton()
wrapper.appendChild(likeButton.render())
likeButton.onStateChange = (oldEl,newEl) => {
// wrapper.insertBefore(newEl,oldEl)
// wrapper.removeChild(oldEl)
wrapper.removeChild(oldEl)
wrapper.appendChild(newEl)
}
class Component {
constructor(props ={}){
this.props = props
}
setState(state){
const oldEl = this.el
this.state = state
this._renderDOM()
if(this.onStateChange){
this.onStateChange(oldEl,this.el)
}
}
_renderDOM(){
this.el = createDOMFromString(this.render())
if(this.onClick){
this.el.addEventListener('click',this.onClick.bind(this))
}
return this.el
}
}
const mount = (component,wrapper) => {
wrapper.appendChild(component._renderDOM())
component.onStateChange = (oldEl,newEl) => {
wrapper.insertBefore(newEl,oldEl)
wrapper.removeChild(oldEl)
}
}
class LikeButton extends Component {
constructor(props){
super(props)
this.state = {isLiked:false}
}
onClick(){
this.setState({
isLiked:!this.state.isLiked
})
}
render(){
return `
<button class="like-btn" style="background-color:${this.props.bgColor}">
<span class="like-text">${this.state.isLiked ? '取消' : '点赞'}</span>
</button>
`
}
}
mount(new LikeButton({
bgColor:'red'
}),wrapper)
</script>
</body>
</html>