From 24c2616a97d05c06098abd67a91736698ab77a0e Mon Sep 17 00:00:00 2001 From: Matt Kirby <MattJKirby@outlook.com> Date: Sun, 16 Apr 2023 21:01:46 +0100 Subject: [PATCH] Added debounce hook --- daily-thought-frontend/src/hooks/useDebounce.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 daily-thought-frontend/src/hooks/useDebounce.ts diff --git a/daily-thought-frontend/src/hooks/useDebounce.ts b/daily-thought-frontend/src/hooks/useDebounce.ts new file mode 100644 index 00000000..c55868fa --- /dev/null +++ b/daily-thought-frontend/src/hooks/useDebounce.ts @@ -0,0 +1,17 @@ +import { useEffect, useState } from 'react' + +function useDebounce<T>(value: T, delay?: number): T { + const [debouncedValue, setDebouncedValue] = useState<T>(value) + + useEffect(() => { + const timer = setTimeout(() => setDebouncedValue(value), delay || 500) + + return () => { + clearTimeout(timer) + } + }, [value, delay]) + + return debouncedValue +} + +export default useDebounce \ No newline at end of file -- GitLab