Skip to content

Instantly share code, notes, and snippets.

@achepukov
Last active November 12, 2018 10:12
Show Gist options
  • Save achepukov/e53eb9f02773021fbfc10b597eb0c9bd to your computer and use it in GitHub Desktop.
Save achepukov/e53eb9f02773021fbfc10b597eb0c9bd to your computer and use it in GitHub Desktop.
React form component using html form
import React from 'react';
export default class extends React.PureComponent {
onSubmit = (event) => {
event.preventDefault();
const { action, method, elements } = event.target;
const data = Array.from(elements).reduce( (result, currentElement) => ({
[currentElement.id]: currentElement.value,
...result
}), {});
const this.props.apiCall(action, method, data);
return false;
}
render() {
return (
<form onChange={this.onChange} onSubmit={this.onSubmit} action='/api' method='post'>
<label htmlFor='test1'>Test 1</label>
<input type='text' id='test1' name='test1'/>
<br /><br />
<label htmlFor='test2'>Test 2</label>
<input type='text' id='test2' name='test2'/>
<br />
<br />
Sure? <input type='checkbox' id='some-check' value='1'/>
<br />
<br />
<input type='submit' />
</form>
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment