Skip to content

Instantly share code, notes, and snippets.

@ahmadabdul3
Last active October 16, 2019 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadabdul3/e58a835dec920dd5de8948f28838fc8a to your computer and use it in GitHub Desktop.
Save ahmadabdul3/e58a835dec920dd5de8948f28838fc8a to your computer and use it in GitHub Desktop.
class SomeForm extends Component {
state = {
email: '',
password: '',
};
// - using arrow functions will auto-bind to 'this', so no need
// for doing this.updateFormField = this.updateFormField.bind(this)
// in the constructor (although there are some caveats to this
// approach when writing unit tests)
updateFormField = (e) => {
const { name, value } = e.target;
this.setState({ [name]: value });
};
submit = (e) => {
const { email, password } = this.state;
// submit the form...
};
render() {
const { email, password } = this.state;
return (
<form onSubmit={this.submit}>
<input value={email} name='email' onChange={this.updateFormField} />
<input value={password} name='password' onChange={this.updateFormField} />
</form>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment