Last active
June 3, 2020 16:59
-
-
Save jrwebdev/1f6e3f12e9a317b69b9b9cd8ec7fcc1c 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
| import { Subtract } from 'utility-types'; | |
| export interface InjectedCounterProps { | |
| value: number; | |
| onIncrement(): void; | |
| onDecrement(): void; | |
| } | |
| interface MakeCounterState { | |
| value: number; | |
| } | |
| const makeCounter = <P extends InjectedCounterProps>( | |
| Component: React.ComponentType<P> | |
| ) => | |
| class MakeCounter extends React.Component< | |
| Subtract<P, InjectedCounterProps>, | |
| MakeCounterState | |
| > { | |
| state: MakeCounterState = { | |
| value: 0, | |
| }; | |
| increment = () => { | |
| this.setState(prevState => ({ | |
| value: prevState.value + 1, | |
| })); | |
| }; | |
| decrement = () => { | |
| this.setState(prevState => ({ | |
| value: prevState.value - 1, | |
| })); | |
| }; | |
| render() { | |
| return ( | |
| <Component | |
| {...this.props as P} | |
| value={this.state.value} | |
| onIncrement={this.increment} | |
| onDecrement={this.decrement} | |
| /> | |
| ); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment