From 7ef2e2659004c4730e6f04146c5b9ffbd2ab54ab Mon Sep 17 00:00:00 2001 From: SWARVY Date: Sat, 17 Aug 2024 23:35:46 +0900 Subject: [PATCH] feat(time): create util hook 'useDebounce' (#97) --- apps/time/src/shared/hooks/useDebounce.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 apps/time/src/shared/hooks/useDebounce.ts diff --git a/apps/time/src/shared/hooks/useDebounce.ts b/apps/time/src/shared/hooks/useDebounce.ts new file mode 100644 index 00000000..0790b997 --- /dev/null +++ b/apps/time/src/shared/hooks/useDebounce.ts @@ -0,0 +1,19 @@ +import { useEffect, useState } from 'react'; + +interface UseDebounceParams { + value: unknown; + delay: number; +} + +export default function useDebounce({ value, delay }: UseDebounceParams) { + const [debouncedValue, setDebouncedValue] = useState(value); + + useEffect(() => { + const delayDebounceTimer = setTimeout(() => { + setDebouncedValue(value); + }, delay); + return () => clearTimeout(delayDebounceTimer); + }, [value, delay]); + + return debouncedValue; +}