Skip to content

Instantly share code, notes, and snippets.

@tyrostone
Created April 24, 2017 13:43
Show Gist options
  • Save tyrostone/a7293816bec9dba34724b0b0f37c7091 to your computer and use it in GitHub Desktop.
Save tyrostone/a7293816bec9dba34724b0b0f37c7091 to your computer and use it in GitHub Desktop.
const Card = (props) => {
return (
<div style={{margin: '1em'}}>
<img width="75" src={props.avatar_url} />
<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 key={card.id} {...card} />)}
</div>
);
};
class Form extends React.Component {
state = {username: ''}
handleSubmit = (event) => {
event.preventDefault();
axios.get(`https://api.github.com/users/${this.state.username}`).then(resp => {
this.props.submitCard(resp.data);
this.setState({ username: '' });
});
};
render() {
return(
<form onSubmit={this.handleSubmit}>
<input type="text"
value={this.state.username}
onChange={(event) => this.setState({ username: event.target.value})}
placeholder="Github username" required />
<button type="submit">Add card</button>
</form>
)
}
}
class App extends React.Component {
state = {
cards: []
};
addNewCard = (cardInfo) => {
this.setState(prevState => ({
cards: prevState.cards.concat(cardInfo)
}));
};
render() {
return (
<div>
<Form submitCard={this.addNewCard} />
<CardList cards={this.state.cards} />
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment