-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data-basics.js
76 lines (66 loc) · 1.75 KB
/
Data-basics.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
68
69
70
71
72
73
74
75
76
const Card = (props) => {
return (
<div style={{margin: '1em'}}>
<img src={props.avatar_url} width="75"/>
<div style={{display:'inline-block', marginLeft: 10}}>
<div style={{fontSize: '1.25em', fontWeight: 'bold'}}>{props.name}</div>
<div>{props.company}</div>
</div>
</div>
)
}
const CardList = (props) => {
return (
<div>
{props.cards.map(card => <Card {...card}/>)}
</div>
);
}
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {username: 'derek-mehlhorn'};
}
handleChange = (event) => {
this.setState({username: event.target.value});
}
handleSubmit = (event) => {
event.preventDefault(); // mark event as handled.
axios.get(`https://api.github.com/users/${this.state.username}`)
.then(result => {
this.props.callback(result.data); // Invoke callback
this.setState({ username: ''});
});
};
render() {
return (
<form onSubmit={this.handleSubmit}>
<input type="text"
value={this.state.username}
placeholder="username"
onChange={this.handleChange} />
<button type="submit">Add card</button>
</form>
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = { data: [] };
}
addUser = (result) => {
this.setState((state, props) => {
return state.data.push(result);
});
};
render() {
return (
<div>
<Form data={this.state.data} callback={this.addUser}/>
<CardList cards={this.state.data} />
</div>
);
}
}
ReactDOM.render(<App />, mountNode);