Created
November 15, 2019 17:58
-
-
Save BretCameron/12f53a3fd53d50f2bdb5787718e107cf to your computer and use it in GitHub Desktop.
A simple counter, made using React classes and typescript
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, { 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