Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Created November 15, 2019 17:58
Show Gist options
  • Save BretCameron/12f53a3fd53d50f2bdb5787718e107cf to your computer and use it in GitHub Desktop.
Save BretCameron/12f53a3fd53d50f2bdb5787718e107cf to your computer and use it in GitHub Desktop.
A simple counter, made using React classes and typescript
import React, { Component } from "react";
interface Props {
title: string;
initialCount: number;
}
interface State {
count: number;
}
class ClassCounter extends Component<Props, State> {
state = {
count: this.props.initialCount
};
add = (factor = 1) => {
const { count } = this.state;
this.setState({ count: count + factor });
};
render() {
const { title } = this.props;
const { count } = this.state;
return (
<div>
<h1>{title}</h1>
<h2>{count}</h2>
<button onClick={() => this.add()}>+</button>
<button onClick={() => this.add(-1)}>-</button>
</div>
);
}
}
export default ClassCounter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment