Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pau1m/21e7ca15897d6dcd9bf8600b47d38369 to your computer and use it in GitHub Desktop.
Save pau1m/21e7ca15897d6dcd9bf8600b47d38369 to your computer and use it in GitHub Desktop.
React. Form with dynamic inputs that setState for values and submit
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