From 9379abf7a9ccbc1a5a577eb468f20dbb267f31dc Mon Sep 17 00:00:00 2001 From: hudy9x <95471659+hudy9x@users.noreply.github.com> Date: Fri, 1 Dec 2023 20:00:57 +0700 Subject: [PATCH] feat: create useDebounce hook (#92) --- packages/ui-app/app/_hooks/useDebounce.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 packages/ui-app/app/_hooks/useDebounce.ts diff --git a/packages/ui-app/app/_hooks/useDebounce.ts b/packages/ui-app/app/_hooks/useDebounce.ts new file mode 100644 index 00000000..549c01c2 --- /dev/null +++ b/packages/ui-app/app/_hooks/useDebounce.ts @@ -0,0 +1,19 @@ +import { DependencyList, useEffect, useRef } from "react" + +export const useDebounce = (cb: () => void, dependencies: DependencyList, time?: number) => { + const timeout = useRef(0) + + useEffect(() => { + timeout.current = setTimeout(() => { + cb() + }, time || 300) as unknown as number + + return () => { + if (timeout.current) { + console.log('debounce run') + clearTimeout(timeout.current) + } + } + }, dependencies) +} +