Last active
December 25, 2018 12:14
-
-
Save jackyef/4857070dd9990276a42b9f29faa5aa99 to your computer and use it in GitHub Desktop.
This file contains 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
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