Skip to content

Instantly share code, notes, and snippets.

@codeAdrian
Last active May 31, 2018 16:52
Show Gist options
  • Save codeAdrian/e84e4ab7412d7c1ef48ebbd6090d8656 to your computer and use it in GitHub Desktop.
Save codeAdrian/e84e4ab7412d7c1ef48ebbd6090d8656 to your computer and use it in GitHub Desktop.
React HOC for forms that keeps track of input values and stores it in state dynamically, handles submit events
import React, { Component } from "react";
const WithForm = (FormFields, action) => {
return class Form extends Component {
handleOnChange = event => {
const inputTarget = event.currentTarget || event.srcElement;
const { title, value } = inputTarget;
this.setState(
{
[title]: value
}
);
};
handleOnSubmit = event => {
event.preventDefault();
action({ ...this.state });
};
render() {
return (
<form className="formMain" onSubmit={this.props.submitHandler}>
<FormFields
handleOnChange={this.handleOnChange}
handleOnSubmit={this.handleOnSubmit}
/>
</form>
);
}
};
};
export default WithForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment