Last active
March 5, 2018 18:46
-
-
Save barbaromatrix/2f31cfc9b03228712acf109d684452f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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