summaryrefslogtreecommitdiff
path: root/hooks/use-debounce.ts
diff options
context:
space:
mode:
Diffstat (limited to 'hooks/use-debounce.ts')
-rw-r--r--hooks/use-debounce.ts15
1 files changed, 15 insertions, 0 deletions
diff --git a/hooks/use-debounce.ts b/hooks/use-debounce.ts
new file mode 100644
index 00000000..5c0b504a
--- /dev/null
+++ b/hooks/use-debounce.ts
@@ -0,0 +1,15 @@
+import * as React from "react"
+
+export function useDebounce<T>(value: T, delay?: number): T {
+ const [debouncedValue, setDebouncedValue] = React.useState<T>(value)
+
+ React.useEffect(() => {
+ const timer = setTimeout(() => setDebouncedValue(value), delay ?? 500)
+
+ return () => {
+ clearTimeout(timer)
+ }
+ }, [value, delay])
+
+ return debouncedValue
+}