Skip to content

Instantly share code, notes, and snippets.

@jrwebdev
Last active June 3, 2018 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrwebdev/b8fea9723d4f86743c29a1888dbbe090 to your computer and use it in GitHub Desktop.
Save jrwebdev/b8fea9723d4f86743c29a1888dbbe090 to your computer and use it in GitHub Desktop.
interface InjectedCounterProps {
value: number;
onIncrement(): void;
onDecrement(): void;
}
interface MakeCounterProps {
minValue?: number;
maxValue?: number;
children(props: InjectedCounterProps): JSX.Element;
}
interface MakeCounterState {
value: number;
}
class MakeCounter extends React.Component<MakeCounterProps, MakeCounterState> {
state: MakeCounterState = {
value: 0,
};
increment = () => {
this.setState(prevState => ({
value:
prevState.value === this.props.maxValue
? prevState.value
: prevState.value + 1,
}));
};
decrement = () => {
this.setState(prevState => ({
value:
prevState.value === this.props.minValue
? prevState.value
: prevState.value - 1,
}));
};
render() {
return this.props.children({
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