Last active
December 25, 2018 07:00
-
-
Save reciosonny/caa63b2d69de3a28a6188fea9a654afd to your computer and use it in GitHub Desktop.
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 initialState = { | |
| numberOfPeople: 10, | |
| slicesPerPerson: 2, | |
| }; | |
| //We then use this HOC like so: WithCalculator(**component goes here**) | |
| //This makes use of arrow functions(=>) and anonymous class construction(returns `class`) | |
| const WithCalculator = WrappedComponent => { | |
| return class extends Component { | |
| static displayName = `WithCalculator(${WrappedComponent.displayName || | |
| WrappedComponent.name})`; | |
| state = { ...initialState }; | |
| updateNumberOfPeople = event => { | |
| const numberOfPeople = parseInt(event.target.value, 10); | |
| this.setState({ numberOfPeople }); | |
| }; | |
| updateSlicesPerPerson = event => { | |
| const slicesPerPerson = parseInt(event.target.value, 10); | |
| this.setState({ slicesPerPerson }); | |
| }; | |
| reset = event => { | |
| this.setState({ ...initialState }); | |
| }; | |
| render() { | |
| const { numberOfPeople, slicesPerPerson } = this.state; | |
| const numberOfPizzas = calculatePizzasNeeded( | |
| numberOfPeople, | |
| slicesPerPerson, | |
| ); | |
| return ( | |
| <WrappedComponent | |
| numberOfPeople={numberOfPeople} | |
| slicesPerPerson={slicesPerPerson} | |
| numberOfPizzas={numberOfPizzas} | |
| updateNumberOfPeople={this.updateNumberOfPeople} | |
| updateSlicesPerPerson={this.updateSlicesPerPerson} | |
| /> | |
| ); | |
| } | |
| }; | |
| }; |
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 WithCalculator = WrappedComponent => { | |
| return class extends Component { | |
| static displayName = `WithCalculator(${WrappedComponent.displayName || | |
| WrappedComponent.name})`; | |
| state = { ...initialState }; | |
| updateNumberOfPeople = event => { | |
| const numberOfPeople = parseInt(event.target.value, 10); | |
| this.setState({ numberOfPeople }); | |
| }; | |
| updateSlicesPerPerson = event => { | |
| const slicesPerPerson = parseInt(event.target.value, 10); | |
| this.setState({ slicesPerPerson }); | |
| }; | |
| reset = event => { | |
| this.setState({ ...initialState }); | |
| }; | |
| render() { | |
| const { numberOfPeople, slicesPerPerson } = this.state; | |
| const numberOfPizzas = calculatePizzasNeeded( | |
| numberOfPeople, | |
| slicesPerPerson, | |
| ); | |
| return ( | |
| <WrappedComponent | |
| numberOfPeople={numberOfPeople} | |
| slicesPerPerson={slicesPerPerson} | |
| numberOfPizzas={numberOfPizzas} | |
| updateNumberOfPeople={this.updateNumberOfPeople} | |
| updateSlicesPerPerson={this.updateSlicesPerPerson} | |
| /> | |
| ); | |
| } | |
| }; | |
| }; |
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
| //normal cons | |
| function test (){ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment