Last active
February 20, 2018 15:35
-
-
Save bad6e/c168ac377dbb4dc23b9869f32bfaaf01 to your computer and use it in GitHub Desktop.
Basic-Form-Refactored
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 BasicFormRefactored extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
firstName: '', | |
age: '', | |
} | |
} | |
handleChange = (event) => { | |
const { target: { name, value } } = event | |
this.setState({ [name]: value }) | |
} | |
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.handleChange} /> | |
</label> | |
<label> | |
Age: | |
<input | |
name="age" | |
type="number" | |
value={this.state.age} | |
onChange={this.handleChange} /> | |
</label> | |
<input type="submit" value="Submit" /> | |
</form> | |
); | |
} | |
} | |
export { BasicFormRefactored } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment