Created
November 14, 2017 08:58
-
-
Save hachibeeDI/894a42597a65531fb80c037428cb4e11 to your computer and use it in GitHub Desktop.
I guess you should use recompose
This file contains hidden or 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 withStateToggle = (name, defaultValue) => ComposedChild => ( | |
class Container extends Component { | |
constructor (props) { | |
super(props); | |
this.state = { | |
[name]: defaultValue | |
}; | |
this.toggle = this.toggle.bind(this); | |
this.enable = this.enable.bind(this); | |
this.disable = this.disable.bind(this); | |
} | |
toggle () { | |
this.setState({[name]: !this.state[name]}); | |
} | |
enable () { | |
this.setState({[name]: true}); | |
} | |
disable () { | |
this.setState({[name]: false}); | |
} | |
render () { | |
const handlers = { | |
[name]: this.state[name], | |
[`${name}Toggle`]: this.toggle, | |
[`${name}Enable`]: this.enable, | |
[`${name}Disable`]: this.disable | |
}; | |
return ( | |
<ComposedChild | |
{...this.props} | |
{...handlers} | |
/> | |
); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment