Last active
April 25, 2019 13:07
-
-
Save aderbas/0ec1f07425da75c14f93a503159f83ea to your computer and use it in GitHub Desktop.
Handling events on input
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
class HelloForm extends React.Component { | |
state = { | |
credentials: { | |
email: '', | |
pwd: '' | |
} | |
} | |
// input change value (login form) | |
handlerChange = name => event => { | |
this.setState({ | |
credentials: Object.assign(this.state.credentials, { | |
[name]: (event.target.checked)?event.target.checked:event.target.value | |
}) | |
}); | |
}; | |
render() { | |
const { credentials } = this.state; | |
return ( | |
<div> | |
<form autoComplete="off"> | |
<label>Email</label> | |
<input type="text" onChange={this.handlerChange('email')}/> | |
<label>Password</label> | |
<input type="password" onChange={this.handlerChange('pwd')}/> | |
</form> | |
<hr /> | |
{/** debug */} | |
<div>{JSON.stringify(credentials)}</div> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render( | |
<HelloForm />, | |
document.getElementById('container') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment