Skip to content

Instantly share code, notes, and snippets.

@nibro7778
Last active February 23, 2019 05:34
Show Gist options
  • Save nibro7778/a04297c1c70be8450ae99aec095e2ada to your computer and use it in GitHub Desktop.
Save nibro7778/a04297c1c70be8450ae99aec095e2ada to your computer and use it in GitHub Desktop.
Sample application which fetch github user details and show its on UI
const Card = (props) => {
return (
<div>
<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 {...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.onSubmit(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: [
{
avatar_url: "https://avatars0.githubusercontent.com/u/16951150?v=4",
name: "Niraj Trivedi",
company: null
}
]
}
addNewCard = (cardInfo) => {
this.setState(prevState => ({
cards: prevState.cards.concat(cardInfo)
}));
};
render() {
return (
<div>
<Form onSubmit={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