Last active
May 2, 2019 03:56
-
-
Save jlittlejohn/8cab4c8af73938d765a82bc509684463 to your computer and use it in GitHub Desktop.
REACT: Basic Form Component Example
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 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