-
Notifications
You must be signed in to change notification settings - Fork 4
/
components.js
67 lines (59 loc) · 1.25 KB
/
components.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, { Component } from 'react';
export class BeerListContainer extends Component {
constructor(props) {
super(props);
this.state = {
beers: []
};
this.addItem = this.addItem.bind(this);
}
addItem(name) {
this.setState({
beers: [].concat(this.state.beers).concat([name])
});
}
render() {
return (
<div>
<InputArea onSubmit={this.addItem}/>
<BeerList items={this.state.beers}/>
</div>
);
}
}
export class InputArea extends Component {
constructor(props) {
super(props);
this.state = {
text: ''
};
this.setText = this.setText.bind(this);
this.handleClick = this.handleClick.bind(this);
}
setText(event) {
this.setState({text: event.target.value});
}
handleClick() {
this.props.onSubmit(this.state.text);
}
render() {
return (
<div>
<input value={this.state.text} onChange={this.setText}/>
<button onClick={this.handleClick}>Add</button>
</div>
);
}
}
export class BeerList extends Component {
render() {
return (
<ul>
{this.props.items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}
}
BeerList.defaultProps = { items: [] };