Forked from stowball/react-form-set-state-input-and-submit.js
Created
December 28, 2018 13:48
-
-
Save pau1m/21e7ca15897d6dcd9bf8600b47d38369 to your computer and use it in GitHub Desktop.
React. Form with dynamic inputs that setState for values and submit
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 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