Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busypeoples/c8b610b3125d5e7a7853d31b489407d1 to your computer and use it in GitHub Desktop.
Save busypeoples/c8b610b3125d5e7a7853d31b489407d1 to your computer and use it in GitHub Desktop.
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