Created
June 28, 2017 17:01
-
-
Save busypeoples/c8b610b3125d5e7a7853d31b489407d1 to your computer and use it in GitHub Desktop.
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
const enhancedForm = Component => | |
class HigherOrderComponent extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { form: props.initialState } | |
this.handleSubmit = this.handleSubmit.bind(this) | |
this.handleChange = this.handleChange.bind(this) | |
} | |
handleSubmit(e) { | |
e.preventDefault() | |
console.log(JSON.stringify(this.state, null, 4)) | |
} | |
handleChange(e) { | |
const target = e.target | |
const name = target.name | |
const value = target.type === 'checkbox' | |
? target.checked | |
: target.value | |
this.setState(state => ({form: {...state.form, ...{[name] : value}}})) | |
} | |
render() { | |
const { initialState, ...rest } = this.props | |
const { form } = this.state | |
return React.createElement(Component, { | |
state: form, | |
handleSubmit: this.handleSubmit, | |
handleChange: this.handleChange | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment