Last active
February 20, 2018 15:33
-
-
Save bad6e/29ca521954e9a2bd57a2b4573780e2ad to your computer and use it in GitHub Desktop.
Basic Form
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
import React from 'react' | |
class BasicForm extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
firstName: '', | |
age: '', | |
} | |
} | |
handleFirstNameChange = (event) => { | |
const firstName = event.target.value | |
this.setState({ firstName: firstName }) | |
} | |
handleAgeChange = (event) => { | |
const age = event.target.value | |
this.setState({ age: age }) | |
} | |
handleSubmit = (event) => { | |
event.preventDefault() | |
const { | |
firstName, | |
age, | |
} = this.state | |
console.log(`A first name was submitted: ${firstName}. An age was submitted: ${age}`) | |
} | |
render() { | |
return ( | |
<form onSubmit={this.handleSubmit}> | |
<label> | |
First Name: | |
<input | |
name="firstName" | |
type="text" | |
value={this.state.firstName} | |
onChange={this.handleFirstNameChange} /> | |
</label> | |
<label> | |
Age: | |
<input | |
name="age" | |
type="number" | |
value={this.state.age} | |
onChange={this.handleAgeChange} /> | |
</label> | |
<input type="submit" value="Submit" /> | |
</form> | |
); | |
} | |
} | |
export { BasicForm } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment