Skip to content

Instantly share code, notes, and snippets.

@D-Maher
Last active March 16, 2023 23:07
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 D-Maher/a6a13bf1da9de92e6da38f7dc060acd6 to your computer and use it in GitHub Desktop.
Save D-Maher/a6a13bf1da9de92e6da38f7dc060acd6 to your computer and use it in GitHub Desktop.
Custom React hook
import { useState } from "react";
function useCounter(
initialCount: number = 0
): [number, () => void, () => void] {
const [count, setCount] = useState<number>(initialCount);
function increment() {
setCount(count + 1);
}
function decrement() {
setCount(count - 1);
}
return [count, increment, decrement];
}
export default useCounter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment