Skip to content

Instantly share code, notes, and snippets.

@devNoiseConsulting
Created April 14, 2018 18:23
Show Gist options
  • Select an option

  • Save devNoiseConsulting/fe2f3c9b8c0bc9a7d6e89c22824ec27a to your computer and use it in GitHub Desktop.

Select an option

Save devNoiseConsulting/fe2f3c9b8c0bc9a7d6e89c22824ec27a to your computer and use it in GitHub Desktop.
Free React.js Bootcamp, Day 2 Challenge
<!DOCTYPE html>
<html>
<head>
<title>First React App</title>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src='https://unpkg.com/babel-standalone@6/babel.min.js'></script>
</head>
<body>
<div id='app'></div>
<script type='text/babel'>
function FriendsList(props) {
return (
<div>
<ul>
{props.list.map((name, i) => {
return (
<li key={name}>
<span>{name}</span>
<button onClick={() => props.onRemoveFriend(name)}>Remove</button>
<button onClick={() => props.onDeactiveFriend(name)}>
Deactive
</button>
</li>
);
})}
</ul>
</div>
);
}
function InactiveFriendsList(props) {
return (
<div>
<ul>
{props.list.map((name, i) => {
return (
<li key={name}>
<span>{name}</span>
<button onClick={() => props.onActiveFriend(name)}>Active</button>
</li>
);
})}
</ul>
</div>
);
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
friends: ['Jordyn', 'Mikenzi', 'Jake'],
inactiveFriends: [],
input: ''
};
this.handleAddFriend = this.handleAddFriend.bind(this);
this.handleRemoveFriend = this.handleRemoveFriend.bind(this);
this.updateInput = this.updateInput.bind(this);
this.handleClearFriend = this.handleClearFriend.bind(this);
this.handleActiveFriend = this.handleActiveFriend.bind(this);
this.handleDeactiveFriend = this.handleDeactiveFriend.bind(this);
}
handleClearFriend() {
this.setState(currentState => {
return {
friends: [],
inactiveFriends: []
};
});
}
handleAddFriend() {
if (this.state.input.length > 0) {
this.setState(currentState => {
return {
friends: currentState.friends.concat([currentState.input]),
input: ''
};
});
}
}
handleRemoveFriend(name) {
this.setState(currentState => {
return {
friends: currentState.friends.filter(friend => friend != name)
};
});
}
handleActiveFriend(name) {
this.setState(currentState => {
return {
friends: currentState.friends.concat([name]),
inactiveFriends: currentState.inactiveFriends.filter(
friend => friend != name
)
};
});
}
handleDeactiveFriend(name) {
this.setState(currentState => {
return {
friends: currentState.friends.filter(friend => friend != name),
inactiveFriends: currentState.inactiveFriends.concat([name])
};
});
}
updateInput(e) {
const value = e.target.value;
this.setState(() => ({ input: value }));
}
render() {
return (
<div>
<input
type="text"
placeholder="new friend"
value={this.state.input}
onChange={this.updateInput}
/>
<button onClick={this.handleAddFriend}>Submit</button>
<br />
<button onClick={this.handleClearFriend}>Clear All</button>
<h2>Active Friends</h2>
<FriendsList
list={this.state.friends}
onRemoveFriend={this.handleRemoveFriend}
onDeactiveFriend={this.handleDeactiveFriend}
/>
<h2>Inactive Friends</h2>
<InactiveFriendsList
list={this.state.inactiveFriends}
onRemoveFriend={this.handleRemoveFriend}
onActiveFriend={this.handleActiveFriend}
/>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment