Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created September 6, 2017 02:34
Show Gist options
  • Save fakenickels/7d0206817cc0903d4674f1dbf7efd5e6 to your computer and use it in GitHub Desktop.
Save fakenickels/7d0206817cc0903d4674f1dbf7efd5e6 to your computer and use it in GitHub Desktop.
import React from 'react'
export default (stateName, handlerName, defaultValue) => Component =>
class StateHandler extends React.Component {
state = { [stateName]: defaultValue }
constructor(props) {
super(props)
this[handlerName] = (value) => this.setState({ [stateName]: value })
}
render() {
const props = { [handlerName]: this[handlerName], ...this.props, ...this.state}
return <Component {...props} />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment