Forked from tylermcginnis/react-bootcamp-day-2-6.html
Created
April 11, 2018 16:56
-
-
Save coder4cbus/bc53710b7a71549f6d52b9938e8254b7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!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 ( | |
<ul> | |
{props.list.map((name) => ( | |
<li key={name}> | |
<span>{name}</span> | |
<button onClick={() => props.onRemoveFriend(name)}>X</button> | |
</li> | |
))} | |
</ul> | |
) | |
} | |
class App extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
friends: ['Jordyn', 'Mikenzi', 'Jake'], | |
input: '', | |
} | |
this.handleRemoveFriend = this.handleRemoveFriend.bind(this) | |
this.updateInput = this.updateInput.bind(this) | |
this.handleAddFriend = this.handleAddFriend.bind(this) | |
} | |
handleAddFriend() { | |
this.setState((currentState) => { | |
return { | |
friends: currentState.friends.concat([this.state.input]), | |
input: '' | |
} | |
}) | |
} | |
handleRemoveFriend(name) { | |
this.setState((currentState) => { | |
return { | |
friends: currentState.friends.filter((friend) => friend !== 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> | |
<FriendsList | |
list={this.state.friends} | |
onRemoveFriend={this.handleRemoveFriend} | |
/> | |
</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