Skip to content

Instantly share code, notes, and snippets.

@jlittlejohn
Last active May 2, 2019 03:56
Show Gist options
  • Save jlittlejohn/8cab4c8af73938d765a82bc509684463 to your computer and use it in GitHub Desktop.
Save jlittlejohn/8cab4c8af73938d765a82bc509684463 to your computer and use it in GitHub Desktop.
REACT: Basic Form Component Example
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ""
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
value: event.target.value
});
}
handleFormSubmit(event) {
alert(this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={() => this.handleFormSubmit()}>
<div className="form-group">
<label>
Name:
<input
type="text"
placeholder="Enter Your Name"
value={this.state.value}
onChange={this.handleChange}
/>
</label>
</div>
<button type="submit">Submit</button>
</form>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment