-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.jsx
103 lines (88 loc) · 2.07 KB
/
app.jsx
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
var PLAYERS = [
{
name: "Jim Hoskins",
score: 31,
id: 1,
},
{
name: "Andrew Chalkley",
score: 35,
id: 2,
},
{
name: "Alena Holligan",
score: 42,
id: 3,
},
];
function Header(props) {
return (
<div className="header">
<h1>{props.title}</h1>
</div>
);
}
Header.propTypes = {
title: React.PropTypes.string.isRequired,
};
var Counter = React.createClass({
propTypes : {},
getInitialState: function(){
return {score: 0,}
},
incrementScore: function() {
this.setState({score: (this.state.score + 1)})
},
decrementScore: function() {
this.setState({score: (this.state.score - 1)})
},
render : function() {
return (
<div className="counter">
<button className="counter-action decrement" onClick={this.decrementScore}> - </button>
<div className="counter-score"> {this.state.score} </div>
<button className="counter-action increment" onClick={this.incrementScore}> + </button>
</div>
);
}
});
function Player(props) {
return (
<div className="player">
<div className="player-name">
{props.name}
</div>
<div className="player-score">
<Counter />
</div>
</div>
);
}
Player.propTypes = {
name: React.PropTypes.string.isRequired,
score: React.PropTypes.number.isRequired,
};
function Application(props) {
return (
<div className="scoreboard">
<Header title={props.title} />
<div className="players">
{props.players.map(function(player) {
return <Player name={player.name} score={player.score} key={player.id} />
})}
</div>
</div>
);
}
Application.propTypes = {
title: React.PropTypes.string,
players: React.PropTypes.arrayOf(React.PropTypes.shape({
name: React.PropTypes.string.isRequired,
score: React.PropTypes.number.isRequired,
id: React.PropTypes.number.isRequired,
})).isRequired,
};
Application.defaultProps = {
title: "Scoreboard",
}
ReactDOM.render(<Application players={PLAYERS}/>, document.getElementById('container'));