-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
91 lines (87 loc) · 2.49 KB
/
index.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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>React Practice</title>
<link rel="stylesheet" href="css/styles.css">
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
(() => {
function Counter(props) {
return (
<li style={{backgroundColor:props.counter.color}} onClick={() => props.countUp(props.counter)}>
{props.counter.id}-({props.counter.count})
</li>
);
}
function CounterList(props) {
const counters = props.counters.map(counter => {
return (
<Counter
counter={counter}
key={counter.id}
countUp={props.countUp}
/>
);
});
return (
<ul>
{counters}
</ul>
);
}
class App extends React.Component {
constructor() {
super();
this.state = {
counters: [
{id: 'A', count: 0, color: 'tomato'},
{id: 'B', count: 0, color: 'skyblue'},
{id: 'C', count: 0, color: 'limegreen'},
{id: 'D', count: 0, color: 'gray'},
{id: 'E', count: 0, color: 'pink'}
],
total: 0
};
this.countUp = this.countUp.bind(this);
}
countUp(counter) {
this.setState(prevState => {
const counters = prevState.counters.map(counter => {
return {id: counter.id, count: counter.count, color: counter.color};
});
const pos = counters.map(counter => {
return counter.id;
}).indexOf(counter.id);
counters[pos].count++;
return {
counters: counters,
total: prevState.total + 1
};
});
}
render() {
return (
<div className="container">
<CounterList
counters={this.state.counters}
countUp={this.countUp}
/>
<div>TOTAL INVENTORY: {this.state.total}</div>
</div>
);
}
}
ReactDOM.render(
<App/>,
document.getElementById('root')
);
})();
</script>
</body>
</html>