-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.html
94 lines (84 loc) · 3.26 KB
/
Counter.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
<!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>React</title>
<script src="React.js"></script>
<script src="ReactDOM.js"></script>
<script src="babel-6.15.0.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const rootElement = document.getElementById("root");
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
number: 0,
luckyNumbers: [],
};
this.getLuckyNumber = this.getLuckyNumber.bind(this);
this.deleteLuckyNumber = this.deleteLuckyNumber.bind(this);
}
componentDidMount() {
this.intervalId = setInterval(() => this.tick(), 1);
}
tick() {
this.setState(function(prevState) {
return {
number: prevState.number + 1,
}
});
}
getLuckyNumber() {
this.setState(function(prevState) {
return {
luckyNumbers: prevState.luckyNumbers.concat(prevState.number)
}
});
}
deleteLuckyNumber(currentNumber) {
return (event) => {
this.setState(function(prevState) {
return {
luckyNumbers: prevState.luckyNumbers.filter(luckyNumber => luckyNumber !== currentNumber)
}
});
}
}
componentWillUnmount() {
clearInterval(this.intervalId);
}
render() {
return (
<div>
<h2>{this.state.number}</h2>
<button onClick={this.getLuckyNumber}>Get Lucky Number</button>
<ul>
{
this.state.luckyNumbers.map((currentNumber, index) => {
return (
<li key={index.toString()}>
{currentNumber}
<button onClick={this.deleteLuckyNumber(currentNumber)}>X</button>
</li>
)
})
}
</ul>
</div>
);
}
}
function App() {
return (
<Counter />
);
}
ReactDOM.render(<App />, rootElement);
</script>
</body>
</html>