Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Created December 17, 2019 09:46
Show Gist options
  • Save eldyvoon/ae041512f34aadc2f9091372b065fe99 to your computer and use it in GitHub Desktop.
Save eldyvoon/ae041512f34aadc2f9091372b065fe99 to your computer and use it in GitHub Desktop.
const CounterHOC = ComposedComponent =>
class extends Component {
constructor(props) {
super(props);
this.state = {
count: props.initCounter
};
}
increase = () =>
this.setState(prevState => ({ count: prevState.count + 1 }));
decrease = () =>
this.setState(prevState => ({ count: prevState.count - 1 }));
render() {
const { count } = this.state;
return (
<ComposedComponent
count={count}
increase={this.increase}
decrease={this.decrease}
{...this.props}
/>
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment