Skip to content

Instantly share code, notes, and snippets.

@Anirudhk94
Created January 12, 2019 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anirudhk94/5d2d9471e40c26dbfdf6e02d6251e9ee to your computer and use it in GitHub Desktop.
Save Anirudhk94/5d2d9471e40c26dbfdf6e02d6251e9ee 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 {...card} key={card.id}/>)}
</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(
<div>
<form onSubmit={this.handleSubmit}>
<input type="text" name="firstname"
placeholder="GitHub Username..."
value = {this.state.userName}
onChange = {(event) => this.setState({userName : event.target.value})}/>
<input type="submit" />
</form>
</div>
)
}
}
class App extends React.Component {
state = {
cards : [
]
}
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