Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Created November 15, 2019 17:58
Show Gist options
  • Save BretCameron/5dcbe894447c3f3abae4296c401b6559 to your computer and use it in GitHub Desktop.
Save BretCameron/5dcbe894447c3f3abae4296c401b6559 to your computer and use it in GitHub Desktop.
A simple counter, made using React functional components and typescript
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