Skip to content

Instantly share code, notes, and snippets.

@hachibeeDI
Created November 14, 2017 08:58
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 hachibeeDI/894a42597a65531fb80c037428cb4e11 to your computer and use it in GitHub Desktop.
Save hachibeeDI/894a42597a65531fb80c037428cb4e11 to your computer and use it in GitHub Desktop.
I guess you should use recompose
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