Skip to content

Instantly share code, notes, and snippets.

@jackyef
Last active December 25, 2018 12:14
Show Gist options
  • Save jackyef/4857070dd9990276a42b9f29faa5aa99 to your computer and use it in GitHub Desktop.
Save jackyef/4857070dd9990276a42b9f29faa5aa99 to your computer and use it in GitHub Desktop.
import React from 'react';
import ReactDOM from 'react-dom';
const withCounter = Component => {
return class ComponentWithCounter extends React.Component {
state = {
count: 0,
};
handleDecrement = () => {
this.setState({ count: this.state.count - 1 });
};
handleIncrement = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
const { count } = this.state;
return (
<Component {...this.props} count={count} onIncrease={this.handleIncrement} onDecrease={this.handleDecrement} />
);
}
};
};
const App = ({ count, onIncrease, onDecrease }) => {
return (
<div>
<div>Current count: {count}</div>
<div>
<button onClick={onDecrease}>-</button>
<button onClick={onIncrease}>+</button>
</div>
</div>
);
};
const AppWithCounter = withCounter(App);
ReactDOM.render(<AppWithCounter />, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment