Created
November 15, 2019 17:58
-
-
Save BretCameron/5dcbe894447c3f3abae4296c401b6559 to your computer and use it in GitHub Desktop.
A simple counter, made using React functional components 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, { FC, useState } from "react"; | |
interface Props { | |
title: string; | |
initialCount: number; | |
} | |
const FunctionalCounter: FC<Props> = ({ title, initialCount }) => { | |
const [count, setCount] = useState(initialCount); | |
const add = (factor = 1) => { | |
setCount(count + factor); | |
}; | |
return ( | |
<div> | |
<h1>{title}</h1> | |
<h2>{count}</h2> | |
<button onClick={() => add()}>+</button> | |
<button onClick={() => add(-1)}>-</button> | |
</div> | |
); | |
}; | |
export default FunctionalCounter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment