Created
March 9, 2016 23:02
-
-
Save colbyr/4ef34f9a56f5de61fcb4 to your computer and use it in GitHub Desktop.
form state management with controlled components and partial application
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
import { partial } from '_'; | |
import API from './API'; | |
export default React.createClass({ | |
getInitialState() { | |
return { | |
username: '', | |
password: '', | |
}; | |
}, | |
handleChange(fieldName, {target: {value}}) { | |
this.setState({[fieldName]: value}); | |
}, | |
handleSave(evt) { | |
evt.preventDefault(); | |
API.login(this.state); | |
}, | |
render() { | |
return ( | |
<form onSubmit={this.handleSave}> | |
<input | |
onChange={partial(this.handleChange, 'username')} | |
placeholder="Username" | |
value={this.state.username} | |
/> | |
<input | |
onChange={partial(this.handleChange, 'password')} | |
placeholder="Password" | |
type="password" | |
value={this.state.password} | |
/> | |
<button type="submit"> | |
Login | |
</button> | |
</form> | |
); | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment