Created
April 10, 2019 17:00
-
-
Save udt1106/d3e906086f9d1d989571c9df7846c425 to your computer and use it in GitHub Desktop.
React.js - Dynamically add and remove GitHub profile in list
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 CardList = (props) => ( | |
<div> | |
{props.profiles.map(profile => <Card key={profile.id} {...profile}/>)} | |
</div> | |
); | |
class Card extends React.Component { | |
render() { | |
const profile = this.props; | |
return ( | |
<div className="github-profile"> | |
<img src={profile.avatar_url} /> | |
<div className="info"> | |
<div className="name">{profile.name} ({profile.login})</div> | |
<div className="company">{profile.company}</div> | |
</div> | |
</div> | |
); | |
} | |
} | |
class FormAdd extends React.Component { | |
state = { userName: '' }; | |
handleSubmit = async (event) => { | |
event.preventDefault(); | |
const resp = await axios.get(`https://api.github.com/users/${this.state.userName}`); | |
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>Add card</button> | |
</form> | |
); | |
} | |
} | |
class FormRemove extends React.Component { | |
state = { userName: '' }; | |
handleSubmit = async (event) => { | |
event.preventDefault(); | |
const resp = await axios.get(`https://api.github.com/users/${this.state.userName}`); | |
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>Remove card</button> | |
</form> | |
); | |
} | |
} | |
class App extends React.Component { | |
state = { | |
profiles: [], | |
}; | |
addNewProfile = (profileData) => { | |
this.setState(prevState => ({ | |
profiles: [...prevState.profiles, profileData], | |
})); | |
console.log({profileData}); | |
}; | |
removeProfile = (profileData) => { | |
this.setState(prevState => ({ profiles: prevState.profiles.filter(data => data.login !== profileData.login) })); | |
//console.log('App', profileData) | |
}; | |
render() { | |
return ( | |
<div> | |
<div className="header">{this.props.title}</div> | |
<FormAdd onSubmit={this.addNewProfile} /> | |
<FormRemove onSubmit={this.removeProfile} /> | |
<CardList profiles={this.state.profiles} /> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render( | |
<App title="The GitHub Cards App" />, | |
mountNode, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment