-
Notifications
You must be signed in to change notification settings - Fork 0
/
React.js
44 lines (44 loc) · 890 Bytes
/
React.js
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
function createDom(domStr) {
const dom = document.createElement("div");
dom.innerHTML = domStr;
return dom;
}
class Component {
setState(state) {
this.state = {
...this.state,
...state
};
const oldEl = this.el;
this._renderDOM();
this.onStateChange(oldEl, this, el);
}
_renderDOM() {
this.el = createDom(this.render());
el.addEventListener("click", this.handleClick);
return this.el;
}
}
class LikeButton extends Component {
constructor() {
this.state = {
isLike: false
};
}
setState(state) {
this.setState(state);
}
handleClick() {
this.setState({
isLike: !this.state.isLike
});
}
render() {
return `
<button class='like-button'>
<span class='like-text'>${this.state.isLike ? "取消" : "点赞"}</span>
<span>👍</span>
</button>
`;
}
}