Skip to content

Instantly share code, notes, and snippets.

@barbaromatrix
Last active March 5, 2018 18:46
Show Gist options
  • Save barbaromatrix/2f31cfc9b03228712acf109d684452f7 to your computer and use it in GitHub Desktop.
Save barbaromatrix/2f31cfc9b03228712acf109d684452f7 to your computer and use it in GitHub Desktop.
const Card = (props) => {
return (
<div>
<img width="100" src={props.avatar_url} />
<div>{props.name}</div>
<div>{props.company}</div>
</div>
);
};
const CardList = (props) => {
return (
<div>
{
props.cards.map(card => <Card {...card} />)
}
</div>
);
};
class Form extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit = (event) => {
event.preventDefault();
axios.get(`https://api.github.com/users/${this.userNameInput.value}`)
.then(resp => {
this.props.onSubmit(resp.data);
})
};
render() {
return(
<form onSubmit={this.handleSubmit}>
<input
ref={(input) => this.userNameInput = input}
placeholder='Github username'
type="text" />
<button type='submit'>Add card</button>
</form>
);
};
};
class App extends React.Component {
constructor() {
super();
this.state = {
list: [
{
avatar_url: 'https://avatars0.githubusercontent.com/u/8710317?v=4',
name: 'Matheus',
company: 'Concrete Solutions'
},
{
avatar_url: 'https://avatars0.githubusercontent.com/u/8710319?v=4',
name: 'Someone',
company: 'Some place'
},
]
};
this.addNewCard = this.addNewCard.bind(this);
}
addNewCard = (cardInfo) => {
this.setState((prevState) => ({
list: prevState.list.concat(cardInfo)
}));
};
render() {
return(
<div>
<Form onSubmit={this.addNewCard} />
<CardList cards={this.state.list} />
</div>
);
}
};
ReactDOM.render(
<App />,
mountNode
);
@barbaromatrix
Copy link
Author

You can test it on Here.
This is a simple implementation of react, getting back the image, name and company from github's user, without checking its information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment