Created
February 26, 2018 04:26
-
-
Save LizhangX/bc4682d3860a2f31f3e81500f7358de7 to your computer and use it in GitHub Desktop.
React Form Example from React
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
class FlavorForm extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {value: 'coconut'}; | |
this.handleChange = this.handleChange.bind(this); | |
this.handleSubmit = this.handleSubmit.bind(this); | |
} | |
handleChange(event) { | |
this.setState({value: event.target.value}); | |
} | |
handleSubmit(event) { | |
alert('Your favorite flavor is: ' + this.state.value); | |
event.preventDefault(); | |
} | |
render() { | |
return ( | |
<form onSubmit={this.handleSubmit}> | |
<label> | |
Pick your favorite La Croix flavor: | |
<select value={this.state.value} onChange={this.handleChange}> | |
<option value="grapefruit">Grapefruit</option> | |
<option value="lime">Lime</option> | |
<option value="coconut">Coconut</option> | |
<option value="mango">Mango</option> | |
</select> | |
</label> | |
<input type="submit" value="Submit" /> | |
</form> | |
); | |
} | |
} | |
ReactDOM.render( | |
<FlavorForm />, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment