Created
February 18, 2017 10:57
-
-
Save stowball/3630da043a8b24b106fefcb93434a713 to your computer and use it in GitHub Desktop.
React. Form with dynamic inputs that setState for values and submit
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 Form extends Component { | |
state = {}; | |
renderInput = (name, type, value='') => | |
<input onChange={({target}) => this.setState({[name]: target.value})} name={name} type={type} value={this.state[name] || value}/>; | |
handleSubmit = (evt) => { | |
evt.preventDefault(); | |
send('/form', this.state); | |
} | |
render() { | |
return <form onSubmit={this.handleSubmit}> | |
{this.renderInput('firstName', 'text')} | |
{this.renderInput('lastName', 'text')} | |
<button type="submit"/> | |
</form>; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment